Javascript "addEventListener" Event Fires on Page Load [duplicate]
el.addEventListener("click", alert("clicktrack"), false);
When this line is executed, the alert
will be called and return undefined
. To pass the alert code you need to wrap it in a function.
el.addEventListener("click", function() { alert("clicktrack"); }, false);
How about:
<script type="text/javascript" language="javascript">
document.write("<div id=\"myDiv\">I am a div</div>");
el = document.getElementById("myDiv");
el.addEventListener("click", function() { alert("clicktrack"); }, false);
</script>