javascript - How can i get all elements with a selector attribute with VanillaJS? -


i'd elements specific data- attribute, need manipulate individually. check code:

html

<div data-something></div> <div data-something></div> 

javascript

document.queryselectorall("[data-something]").onclick = function() {     ... } 

below how can elements selector attribute , create onclick listener each element.

run snippet, , click "click me -- a" or "click me -- b" links demo:

var elements = document.queryselectorall('[data-something]');    array.prototype.foreach.call(elements, function (element) {      element.onclick = function () {           alert(element.innerhtml);       };  });
<div data-something>click me -- a</div>  <div data-something>click me -- b</div>

you can use .addeventlistener('click', somefunction) instead of .onclick = somefunction.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -