var links = document.querySelectorAll('#menu .links[href]'); for (var i = 0, link; link = links[i]; i++){ link.onmouseover = replace; link.onmouseout = replace2; }