I have an array of two objects like this:
x = {
word: "Gun",
pic: "<img id='pic' src='images/stimuli/gun.gif'/>",
}, {
word: "Hammer",
pic: "<img id='pic' src='images/stimuli/hammer.gif'/>",
}
I have to compare the first element of this array via the src attribute to another image tag y:
<img id="pic11" height="115" width="90" src="images/stimuli/gun.gif"/>
And I tried this comparison and it gives "undefined is not a function":
x[0].pic.attr('src') == pic11.src
If I log to console like this: console.log($(x[0].pic).attr('src')) it works I get images/stimuli/gun.gif but I cannot access via the variable x in my script.
In other words, I fail to access the src attribute of the img tag of the object in my array. How do I do that?
$(x[0].pic).attr('src')in your script?x[0].pic.attr('src')? Yourpicis a string, not a jQuery element. Check the console, there should be a nice error in there with some clue.xwas an array of objectsx = [{}, {}];. But the value of x in the example you posted would just be the first objectx = {}, {}console.log($(x[0].pic).attr('src'))it works" ... then use that :)