I must be missing something incredibly simple here, but I cannot for the life of me figure it out. Why does the page below execute the alert on page load, and not on button click as intended? I am attempting to assign the clicky() function to the event listener (this assignment happens at page load), but instead the actual alert happens at page load (i.e. the clicky function gets called), and the button never gets its listener. What am I missing?
<html>
<head>
<script>
function clicky() {
alert("clicky");
}
function start() {
var myButton = document.getElementById("theButton");
myButton.addEventListener("onclick", clicky(), false);
}
window.onload = start;
</script>
</head>
<body>
<input type="button" id="theButton" value="I'm a button!">
</body>
</html>
Try ‘click’ instead of ‘onclick’ in your call to addEventListener.