Adding event listeners with Javascript DOM

burak2461

New Member
I'm having trouble adding eventListener to through javascript. Ok, I have a function that creates 4 anchor elements. I want to add an event to them onmouseover that calls a function to change their backkground color. Here's the code (look at the next to last line of createAnchor() to find the relevant code line.\[code\]function createAanchor(index) { var a = document.createElement("a"); var text = getText(index); var a = document.createElement("a"); var t = document.createTextNode(text); a.href = http://stackoverflow.com/questions/15581004/getHref(index); a.appendChild(t); a.style.textAlign ="center"; a.style.fontSize = "1.2em"; a.style.color = "white"; a.style.fontFamily = "arial"; a.style.fontWeight = "bold"; a.style.textDecoration = "none"; a.style.lineHeight = "238px"; a.style.width = "238px"; a.style.margin = "5px"; a.style.background = eightColors(index); a.style.position = "absolute"; a.addEventListener("onmouseover", changeColor()); return a; } function changeColor() { alert("EVENT WORKING"); }\[/code\]Ok here's the problem. When the function gets to \[code\]a.addEventListener("onmouseover", changeColor());\[/code\] the \[code\]function changeColors()\[/code\] executes, but it does not execute later on \[code\]onmouseover\[/code\] Why is this?
 
Back
Top