Skip to content

Instantly share code, notes, and snippets.

@kevzettler
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevzettler/11384188 to your computer and use it in GitHub Desktop.
Save kevzettler/11384188 to your computer and use it in GitHub Desktop.
JS Assign
  • Look at the following code. What does it do? What happens if you click on the li elements?
  • Establish what happens when you click on the li elements. Why does this happen?.
  • how can the code can be fixed so that it has the expected behavior
<html>
  <body>
    <ul>
     <li>First</li>
     <li>Second</li>
     <li>Third</li>
     <li>Fourth</li>
    </ul>
  </body>

  <script type="text/javascript">
    els = document.getElementsByTagName('li');
    for(i=0; i < els.length; i++){
      els[i].addEventListener('click', function(){alert(i);}, false);
    }
  </script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment