Locking index while looping


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var elems = document.getElementsByTagName( 'a' );
  2.  
  3. for ( var i = 0; i < elems.length; i++ ) {
  4.  
  5. (function( lockedInIndex ){
  6.  
  7. elems[ i ].addEventListener( 'click', function(e){
  8. e.preventDefault();
  9. alert( 'I am link #' + lockedInIndex );
  10. }, 'false' );
  11.  
  12. })( i );
  13.  
  14. }

URL: http://benalman.com/news/2010/11/immediately-invoked-function-expression/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.