5

I have a form that I want to be able to compare the numbers entered in two input boxes and if they match then I want to display a thumbs up image if they don't I want to display a thumbs down image. I'm attempting to use javascript to do this but I'm not sure how to get the images to display. I've searched this topic but am unable to find anything that explains how to display an image using javascript in the manner I need to do it. Here is what I have for my code so far, and I'm not even sure if what I have is even correct,

if document.getElementById("scan_out").value == document.getElementById("scan_in").value 
//do something here to display thumbs_up.png
else document.getElementById("scan_out").value >= document.getElementById("scan_in").value
//do something here to display thumbs_down.png

This is my first attempt at javascript so I'm not sure if I'm on the right track or not?

so this is what I have now as given from answers below

<html>
<head>
</head>

<body>
    <form>
        <input type="text" name="name" id="firstField"/>
        <img src="images/thumbs_up.png" style="display: none;" id="image1"/>
        <img src="images/thumbs_down.png" style="display:block;" id=image2"/>
        <input type="text" name="name" id="secField" onchange="equals()"/>
    </form>
</body>

<script type="text/javascript">
    var firstField = document.getElementById('firstField');
    var secField =   document.getElementById('secField');
    var image =    document.getElementById('image2');
    var image =    document.getElementById('image1');

    function equals() {
        if(firstField.value == secField.value) {
            image.style.display = 'block';
        }
        else {
            image.style.display = 'none';
        }
    }
firstField.onkeyup = function() { equals() }
    secField.onkeyup = function() { equals() }


</script>

but this always displays a thumbs down image then adds the thumbs up image if the inputs match showing both the thumbs up and thumbs down. I don't want there to be an image unless the values match or don't match but only after a value is input.

5
  • Is this all the you've tried? Commented Jul 26, 2013 at 11:01
  • I believe your question may be answered in this thread:stackoverflow.com/questions/554273/… Commented Jul 26, 2013 at 11:01
  • @Pablo208 I've read that thread already and it doesn't answer my question Commented Jul 26, 2013 at 11:04
  • This will help you stackoverflow.com/questions/5451445/… Commented Jul 26, 2013 at 11:06
  • @BindiyaPatoliya Thanks but that doesn't help, I've look at all the suggestions for answers already given on stackoverflow but none of them give a clear answer as to how I can display the image Commented Jul 26, 2013 at 11:25

5 Answers 5

2

clean and easy way with js is following...

after edit

<html>
<head>
</head>

<body>
    <form>
        <input type="text" name="name" id="firstField"/>
        <img src="images/thumbs_up.png" style="display: none; background: #000;" id="image"/>
        <img src="images/thumbs_down.png" style="display: none; background: #ff0000;" id="image2"/>
        <input type="text" name="name" id="secField" onchange="equals()"/>
    </form>
</body>

<script type="text/javascript">
    var firstField = document.getElementById('firstField');
    var secField =   document.getElementById('secField');
    var image =    document.getElementById('image');
    var image2 =    document.getElementById('image2');

    function equals() {
        if(firstField.value == secField.value) {
            image.style.display = 'block';
            image2.style.display = 'none';
        }
        else {
            image.style.display = 'none';
            image2.style.display = 'block';
        }
    }
    firstField.onkeyup = equals;
    secField.onkeyup = equals;


</script>
</html>
Sign up to request clarification or add additional context in comments.

5 Comments

Why do you wrap equals() in an anonymous function? Just do firstField.onkeyup = equals; and also use addEventListener.
thank you :) can you tell me more about addEventListener or give me some link about it??
@maxART Thank you this helps a lot on getting the image to display, just need to work out some stuff like not having the image display until the values are compared.
@maxART Thanks I appreciate the effort but it's still the same, when the page loads it shows thumbs down and when I add the equal values to each box it displays a thumbs up along with the thumbs down. I can't figure out how to remove the thumbs down from the initial loading of the page.
@maxART correction to the above, noticed too late to edit but your changes do in fact work, it only displays the one image now on equal values, just need to figure out how to stop the thumbs down from displaying after a value is entered in the first box but I think removing the onkeyup from the first box will do that. Thank you fro your help
0

Here is an example how can you match numbers.

var n1 = parseInt(document.getElementById('text1').value, 10);
var n2 = parseInt(document.getElementById('text2').value, 10);
var l = document.querySelector('label');
if (n1 == n2)
    l.innerHTML = 'Number1 = Number2';
else
    l.innerHTML = 'Number1 != Number2';

Comments

0

If you are new to JavaScript, you shall definitely use jQuery.

Then the code would be:

if ( $("#scan_out").val() === $("#scan_in").val() ) {
   $("#scan_icon").html('<img src="thumbs_up.png">');
} else {
   $("#scan_icon").html('<img src="thumbs_down.png">');
}

You should place <div id="scan_icon"></div> where you want the icon to appear.

5 Comments

If you're new to JavaScript, you definitely should not use jQuery. Not my downvote though.
@Richard I tried this but it didn't work, I added the code just as you have it and placed the div where I want the image to display but nothing happens when I enter values in the input boxes
@user594112 You must have done some mistake, it works, look at fiddle: jsfiddle.net/cqHtF
@Teemu: Why not? When properly taught, it allows you to write much cleaner code than pure JS. Please explain your objection, I'm really interested in your opinion.
When you are learning the basics of JS and especially DOM, you will get a better understanding how all works, when using pure JS. We can see the consequences of starting with jQuery even here at SO. When a programmer knowing only jQuery gets a job, which must be done without jQuery, we answer here questions like: Why my getElementById is not working? var a = document.getElementById('#foo');
0

Create an empty div with id img1 and use this code

if( document.getElementById("scan_out").value == document.getElementById("scan_in").value)

{ 
     document.getElementById("img1").innerHTML='<img src="path/to/image.png">';
}
    else if( document.getElementById("scan_out").value >= document.getElementById("scan_in").value){
    document.getElementById("img1").innerHTML='<img src="path/to/image2.png">';
}

4 Comments

simple, but effective.
Why did you copy the pseudo code from OP's post? This is not working. Even if you would make this JS, it would always show "thumbdown" or nothing changes.
Well the poster didnt really give us alot to work with. This answer purely how to show and image with javascripts like the poster asked right?
innerHTML is not a function, but a property, FYI.
0
 if(document.getElementById("scan_out").value == document.getElementById("scan_in").value){
     document.getElementById("img_id").src= "images/xyz/someimage.png";
    }
    else if(document.getElementById("scan_out").value == document.getElementById("scan_in").value){
   document.getElementById("img_id1").src= "images/xyz/someimage.png";
}

3 Comments

The same lack of logic here... "if they match then ... thumbs up ... if they don't ... thumbs down".
I have tested it and it has workde..can you point out where i am going wrong?i will delete my answer if your answer is valid...are you talking about replacing div with images or images with images??
First checking == , then >= makes this funny. Sorry for the markup, I'm on a phone now, a thunder storm just arrived here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.