Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

js addeventlistener foreach

var buttons = document.querySelectorAll('.btn-click-me');
for (var i = 0; i < buttons.length; i++) {
    var self = buttons[i];

    self.addEventListener('click', function (event) {  
        // prevent browser's default action
        event.preventDefault();

        // call your awesome function here
        MyAwesomeFunction(this); // 'this' refers to the current button on for loop
    }, false);
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #js #addeventlistener #foreach
ADD COMMENT
Topic
Name
5+2 =