Posted By

arpit on 06/10/11


Tagged


Versions (?)

Locking index while looping


 / Published in: JavaScript
 

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

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

Report this snippet  

You need to login to post a comment.