How do you assign an event handler to multiple elements in JavaScript? -
How do you assign an event handler to multiple elements in JavaScript? -
i know how jquery, , know how event delegation. how do in plain javascript?
for example, how assign event handler bunch of lis?
i see var li = document.queryselectorall('li');. returns array-like thing. loop through , assign handlers? sense there must improve way. if not, what's best way loop through array-like thing (and array-like thing called?)?
yes, should loop through collection , assign handlers individually. jquery behind scenes.
queryselectorall returns nodelist array-like object.
for looping through list, can either utilize for loop:
var list = document.queryselectorall('li'), l = list.length, li; (var = 0; < l; i++) { li = list.item(i); // ... } or utilize foreach method:
[].foreach.call(document.queryselectorall('li'), function(li) { // ... }); javascript javascript-events event-handling
Comments
Post a Comment