Home › Forums › JavaScript › Trying to set the onClick attribute of an element inside a for loop. › Reply To: Trying to set the onClick attribute of an element inside a for loop.
December 20, 2015 at 6:12 pm
#236053
Shikkediel
Participant
You’re actually executing it on every increment of i
instead of setting the attribute as a string :
img.setAttribute('onClick', "fullSize('gallery-images/anime" + i + ".jpg')");
It would be better to use an event listener though, this appears to create the correct markup but doesn’t seem to work either – it’s logging a “not defined” reference error for the fullSize
function.