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.

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #236048
    MMBlast
    Participant

    The onClick function gets executed in every loop and the onClick attribute gets set to undefined. Any detailed explanations are greatly welcomed.

    http://codepen.io/MMEquinox/pen/qbNWqP

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

    #236054
    Shikkediel
    Participant

    I think that happens because all of the script is wrapped in an anonymous (function() {}), making the scope so that the inner can’t be reached with inline scripting.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.