<div class="green">1</div> <div class="green">2</div> <div class="green">3</div> <script> [].forEach.call(document.getElementsByClassName('green'), function(el){ el.onclick = function(){ el.style.backgroundColor = 'green'; }; }); </script>