Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

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.

#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.