function show(ev){ var element = (ev.target||ev.srcElement); element.style.backgroundColor = "palegreen"; } function addAll(){ var item; var checks = document.getElementsByTagName("span"); for (item of checks){ if (item.getAttribute("class") == "check"){ item.addEventListener("click",show,false); } } } window.onload = addAll;