tazo todua tazo todua - 3 months ago 8
jQuery Question

jQuery adds strange class on IMGs (with SRC)

I have noticed, that with:

<img id="smth" src="#" />
...........
...........
...........
$("#smth").src = ".....";


and strange class is added to
img
element:

enter image description here

(this doesnt happen if i dont execute that).
Any thoughts?

Answer
$("#element")

returns an jquery object representing the elements matching the selector. In this case it is an representing a single element. When you intend to set its src like this

$("#element").src = ".....";

you are actually trying to set the src attribute of that jquery object. Instead of that, there are several possible solutions. Examples:

attr

$("#element").attr("src", ".....");

prop

$("#element").prop("src", ".....");

element of array

$("#element")[0].src = ".....";

get

$("#element").get(0).src = ".....";