javascript - Creating multiple event listeners using a loop -


trying create multiple listeners loop. how make work?

var buttons = ['one', 'two', 'tree']; $.each(keys, function(key, value) {     $(value).click(function() {         //     }); }); 

also, there shortcut not writing key, value when need value?

you better off putting delegated event listener on parent instead of iterating through every button. example, if place <button> elements inside of <div> id #container, can write listener this:

$('#container').on('click', 'button', function() {   // something; }); 

now, every time button element clicked within div, callback invoked. can use class selector in place of 'button' listen elements have class.


Comments

Popular posts from this blog

Unlimited choices in BASH case statement -

Redirect to a HTTPS version using .htaccess -

javascript - jQuery: Add class depending on URL in the best way -