1var elements = document.getElementsByClassName("classname");
2
3var myFunction = function() {
4 var attribute = this.getAttribute("data-myattribute");
5 alert(attribute);
6};
7
8for (var i = 0; i < elements.length; i++) {
9 elements[i].addEventListener('click', myFunction, false);
10}
11
12// If you have ES6 support you can replace your last line with:
13
14 Array.from(elements).forEach(function(element) {
15 element.addEventListener('click', myFunction);
16 });
1var classname = document.getElementsByClassName("classname");
2
3var myFunction = function() {
4 var attribute = this.getAttribute("data-myattribute");
5 alert(attribute);
6};
7
8for (var i = 0; i < classname.length; i++) {
9 classname[i].addEventListener('click', myFunction, false);
10}