Here i have four input buttons and i want to add event handler for each of them.If i use for loop i have to deal with closures.Actually i am trying to understand how to manage forEach method for array like object.This is the closest solution i can manage.As we have to encounter closure related problem if we want to use array methods.I had to use object.keys to get the indexes and applied forEach on them.Though it sounds strange but i am not quite satisfied with this solution.Can it be more simpler?How can i manage forEach directly on the nodeList stored in Buttons variable
function change(){
var buttons=document.getElementsByTagName('input');
var keys=Object.keys(buttons);
keys.forEach(function(el,indx,arr){
if(el != 'length'){
this[el].addEventListener('click', function(e){
alert(e.target.value);
});
}
}, buttons);
}
change();
<input type='button' value='button1'>
<input type='button' value='button2'>
<input type='button' value='button3'>
<input type='button' value='button4'>