Reputation: 18818
Once #theButton
is clicked, an alert should be triggered. Why aren't any of the events being triggered using the following methods?
var theButton = document.getElementById("theButton");
theButton.onclick = function(){alert("Clicked!");}
theButton.onclick = clickAlert;
theButton.onclick(alert("Clicked!"));
function clickAlert(){
alert("Clicked!");
}
</script>
</head>
<body>
<input type="button" id="theButton">
</body>
</html>
Upvotes: 0
Views: 274
Reputation: 34623
It works if you put the onclick within the input tag (and spell things right):
<html>
<head>
<script type="text/javascript" language="javascript">
function clickAlert(){
alert("Clicked!");
}
</script>
</head>
<body>
<input type="button" id="theButton" NAME="theButton" onclick="clickAlert()">
</body>
</html>
Upvotes: 1
Reputation: 6776
I like the jQuery.ready()
answer, but I believe you could also move the <script>
block to some point afeter you delcalre the button like at the end of the page.
Upvotes: 1
Reputation: 105914
You have three problems
function
on line 7. (This generates a runtime error, by the way)onclick
property on line 8 - not sure if you're doing that on purpose or whatUpvotes: 2
Reputation: 449783
Because theButton
doesn't exist yet when you run your script?
Wrap it into the body's onload
event or jQuery's ready()
.
Upvotes: 1