0

This is my script

var str1 = "#";
var str2 = unique;
var n = str1.concat(str2);

if (status > 0){
$(n).attr("src", "/img/red.jpg")

}else{
$(n).attr("src", "/img/green.jpg")
}

This is my html- inside a table - the unique code (quite long id?) is the same as us.key() It did work when I used find() within the <tr> tag (Overly complex and prone to errors though) This is simple and should work but isn't? Any ideas what I am doing wrong?

    <td>
    {% if us.status == 0 %}
<img id={{us.key()}} src="/img/green.jpg"></img>
{% else %}
<img     id="{{us.key()}}" src="/img/red.jpg"></img>
{% endif %}
    </td>

UPDATE: If I add the string directly into the scrip it works - somehow the concatenated string is getting changed

Nicolas Henneaux
  • 11,507
  • 11
  • 57
  • 82
user2764108
  • 146
  • 2
  • 10

2 Answers2

2

Try doing your jQuery code properly like this:

(function ($) {
    "use strict";
    // Based on your original code, I'm assuming that both the 'unique' and 'status' variables are global
    var str = unique;
    if (status > 0) {
        $('#' + str).prop("src", "/img/red.jpg");
    } else {
        $('#' + str).prop("src", "/img/green.jpg");
    }
}(jQuery));

Please take note that you should be using the .prop() method instead of .attr(). Using prop is the recommended way.

Cliffmeister
  • 161
  • 10
  • Thanks for putting me straight, I assumed .attr() was the correct method. Yes status and unique are global strings – user2764108 Oct 16 '13 at 11:47
0

you can try

var str1 = "#";
var str2 = unique;


//  var n = str1.concat(str2);

if (status > 0){
$("#"+str2 ).attr("src", "/img/red.jpg")

}else{
$("#"+str2 ).attr("src", "/img/green.jpg")
}
Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55
  • Thanks for the answer but it is still not setting the scr property. All I can think is that the id is too long and maybe has non standard characters. An example unique code could be - ag1kZXZ-cGV0ZXJ0ZXN0chULEghFbXBsb3llZRiAgICAgLzyCgw - maybe this is the problem? – user2764108 Oct 16 '13 at 08:54
  • http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html – Rituraj ratan Oct 16 '13 at 08:57
  • see in console that attr is set or not or your path of image is right? – Rituraj ratan Oct 16 '13 at 08:58