4

I have the following in place:

    var diff = maxval - ui.value;
    $( "#output").html( diff?"diff. of " + diff : "no diff" ); 

Now I'd like to add an image to each, if there's a difference in value or not, ie:

    var diff = maxval - ui.value;
    $( "#output").html( diff?"<img src='yes.png' height='50' width='50' /> diff. of " + diff : "<img src='no.png' height='50' width='50' /> no diff" ); 

Seeing as though that doesn't work, how can I set an image for each in that output div?

3
  • What kind of element is "output"? A single div? Commented May 28, 2013 at 14:57
  • What do you mean with 'doesn't work'? Commented May 28, 2013 at 15:06
  • 11684, As in, the images don't show in the single output div @Filippos Karapetis Commented May 28, 2013 at 17:45

2 Answers 2

6

You're checking based on diff, don't you mean to check if it's greater than 0?

Negative numbers evaluate to true.

Boolean(-1);//true
Boolean(50);//true
Boolean(-500);//true
Boolean(-0.001);//true
Boolean(0);//false

Here is how I'd create a new image with a source attribute decided based on diff being greater than 0. Note, I'm using actual elements, so I'm changing the src attribute instead of a string value which I believe creates more readable code.

var diff = maxval - ui.value;
var img = new Image();
img.src = (diff > 0 ) ? "yes.png" : "no.png";
img.width = "50px";
img.height = "50px";
$( "#output").empty().append(img);

Here is a fully vanilla solution, including the text node:

var diff = maxval - ui.value;
var img = new Image();
img.src = (diff > 0 ) ? "yes.png" : "no.png"; // assuming you mean to check positive value
img.width = "50px";
img.height = "50px";
var el = document.getElementById("output");
el.appendChild(img);
var text = (diff > 0) ? "diff" : "no diff";
var txt = document.createTextNode(text);
el.appendChild(txt);

While the advantages of this 'longer' code don't seem obvious at first, it is very easy to manipulate. I'm working with DOM elements directly instead of strings, I can easily add or remove attributes, change properties, clone them, etc.

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks Benjamin. What about displaying the img and then the text in the output div?
@Dave That's what appendChild does in the native DOM version, and append does in the jQuery version. It appends an element to the output div (That is, a div with an ID value of output).
Correct, trying to check positive values. Can you provide an example please with the img and diff. of " + diff text and the other?
Never mind :) just saw the updated answer, sorry! thank you!!
@Dave you're very welcome, being effective with JavaScript without having to rely on jQuery makes you a better developer :)
|
0

I am not sure if I understand correctly the question, if so, at least this could guide you a little bit:

var yes = $( "<img>").attr( {"src": 'yes.png', "height": '50', "width": '50' });
var no = $( "<img>").attr( {"src": 'no.png', "height": '50', "width": '50' });

if(diff){
    $( "#output").append(yes).append('diff. of ' + diff);
}
else{
    $( "#output").append(no).append('no diff');
}

Example: http://jsfiddle.net/WKg3A/

2 Comments

You understood correctlt, thank you. However, on sliding it keeps adding the output (append) line after line on slider movement, and no images displayed :( same issue as benjamin's code above - can you please provide a jsfiddle? Thanks
Of course, take a look here: jsfiddle.net/WKg3A Justo toggle the value of diff to true or false to see different images

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.