Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call function with className.onclick JS

var el = document.getElementsByClassName("elements"); // this element contains more than 1 DOMs.
el.onclick = function() { console.log("target name should be here")};

So I don't want to have inline JS, I want to fire function when it click to a specific DOM that has same class to any other elements.
Could anyone help please?

like image 266
Minh Bang Avatar asked May 26 '26 13:05

Minh Bang


1 Answers

You cannot attach an event listener to an array of objects, which is that function will be returning.

Instead, loop through the list returned and attach the event listener to each item.

like image 153
AvetisG Avatar answered May 28 '26 01:05

AvetisG