I’m replacing some code in an old application, and instead of getting the node by it’s id I get it by it’s class. Because there isn’t a good way to get a node by it’s class in native JS I do it with jQuery, however the new code doesn’t change the img src.
Is there an obvious reason?
Old code:
var gamearea = document.getElementById('gamearea')
var images = gamearea.getElementsByTagName('img');
images[place] = "pics/" + id + ".png";
New code:
$('.gamearea').children('img').eq(place).src = "pics/" + id + ".png";
.eq()returns a jQuery object. If you want to use the.srcproperty directly, you need a DOM element (not a jQuery object) for which you would use.get()instead of.eq()like this:I also switched to
.find()instead of.children()to be more equivalent to.getElementsByTagName()that you had in the original version and since you didn’t show us your HTML, it seems a safer assumption.