DOM onclick event not working in form?
A simple thing but i can't get it work.
What i want to achieve is just being able to get that message box when u click on the submit button in the form.
When i use the script below and i load the html page i immediately get the popup box and when i click the button nothing happens :cry: .
Can somebody help me out?
Code:
<html>
<body>
<form name="myForm">
The form's name is: <input type="text" name="text1" size="20">
<br /><br />
<input type="button" value="Show the form's name">
<script language="JavaScript" type="text/JavaScript">
document.forms[0].elements[0].onclick=window.alert('You clicked');
</script>
</form>
</body>
</html>
Re: DOM onclick event not working in form?
hi choller,
just do this change and you'll make it.
Code:
<html>
<body>
<form name="myForm">
The form's name is: <input type="text" name="text1" size="20">
<br /><br />
<input type="button" value="Show the form's name" onclick="show()">
<script language="JavaScript" type="text/JavaScript">
function show() {
window.alert('You clicked');
}</script>
</form>
</body>
</html>
Re: DOM onclick event not working in form?
You were accessing the first index of the elements array, which would be the text box, but i'm assuming this is what you were after.
Code:
<html>
<body>
<form name="myForm">
The form's name is: <input type="text" name="text1" size="20">
<br /><br />
<input type="button" value="Show the form's name">
<script language="JavaScript" type="text/JavaScript">
document.forms[0].elements[1].onclick = function()
{
alert('You clicked');
}
</script>
</form>
</body>
</html>
kishore.kr's works just as well. ;)