I have created an element using document.getElementsByClassname, and would like to add a onclick event to this element, so that when someone clicks on this element onclick f
You can use an event delegation or DOMNode.matches.
document.addEventListener('click', function(e) { if (e.target.className == 'classname') // if (e.target.matches('.classname')) { //do something } });