Guest User

Untitled

a guest
May 26th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. // clear the screen for testing
  2. document.body.innerHTML = '';
  3.  
  4. var nums = [1,2,3];
  5.  
  6. // Let's loop over the numbers in our array
  7. for (var i = 0; i < nums.length; i++) {
  8.  
  9. // This is the number we're on...
  10. var num = nums[i];
  11.  
  12. // We're creating a DOM element for the number
  13. var elem = document.createElement('div');
  14. elem.textContent = num;
  15.  
  16. // ... and when we click, alert the value of `num`
  17. elem.addEventListener('click', (function(numCopy) {
  18. return function() {
  19. alert(numCopy);
  20. };
  21. })(num));
  22.  
  23. document.body.appendChild(elem);
  24. };
Add Comment
Please, Sign In to add comment