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
Try this: document.getElementsByClassName always returns array of elements.
document.getElementsByClassName
var element= document.getElementsByClassName('classname'); for(var i=0;i<element.length;i++){ element[i].addEventListener("click", function(){alert('clicked')}, false); }