Posted By

thefascinator on 04/29/15


Tagged

loop for store


Versions (?)

Store var i in for loop and pass to function


 / Published in: JavaScript
 

Pass a function in your for loop the incremental variable in that loop. It stores it and passes it correctly.

  1. for (var i = 1; i <= 99; i++)
  2. {
  3. (function(i)
  4. {
  5. $('.div' + i).on
  6. ({
  7. click: function()
  8. {
  9. // This will pass the correct var for i.
  10. certainFunction(i);
  11. }
  12. });
  13. }(i));
  14. };

Report this snippet  

You need to login to post a comment.