I have a simple button in my HTML. I also have a function in my external JavaScript file. This function calls an alert box with some text. Now, I also have a global variable in my JavaScript that gets the button by id, then assigns the onclick event handler to it which in turn is assigned the call to my alert box function. Simple right?
Problem: The alert box shows up when the page loads as well as when the button is clicked. This is not desirable.
My html:
[QUOTE]
[SIZE=”1″]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”
“
<html>
<head>
[B]<script type=”text/javascript” src=”js.js”>
</script>
</head>
<body>
[B]<form>
<input type=”submit” value=”button” id=”a” />
</form>
</body>
</html>
My JavaScript:
[QUOTE]
[SIZE=”1″]function itClicked()
{
alert(“Hello World!”);
}
[B]
var element = document.getElementById(‘a’);
element.onsubmit = itClicked();
I’m obviously doing this all wrong. Help would be appreciated. Thanks.