waynedunkley
2/1/2016 - 11:09 AM

function definitions inside for loops

function definitions inside for loops

var elements = document.getElementsByTagName('input');

var n = elements.length;    // assume we have 10 elements for this example

var makeHandler = function(num) {  // outer function
     return function() {   // inner function
         console.log("This is element #" + num);
     };
};

for (var i = 0; i < n; i++) {
    elements[i].onclick = makeHandler(i+1);
}