Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onClick="clearForm();return false;" 1

Status
Not open for further replies.

AvaB

Programmer
Jun 23, 2004
16
0
0
CA
Just wondering, in code such as the one below, what exactly does the "return false" do?

Code:
function clearForm(){
 document.form1.firstname.value="";
 document.form2.lastname.value="";
}

Code:
<input type="image" src="clear.jpg" name="clear" onClick="clearForm();return false;">
 
Not true. Because you have an input of type 'image', clicking it, by default, submits the surrounding form.

The 'return false' in this case, prevents the form from submitting.

Here is an example I tried. Substitute your own picture and try it with and without the 'return false' and see what happens.

Code:
<html>
<head>
<script>
function sayHey()
{
 alert('hey!');
}
</script>
</head>
<body>
<form action="[URL unfurl="true"]http://www.google.com/index.html">[/URL]
<input type='image' src='Nic1.bmp' onclick='sayHey();return false;' />
</form>
</body>
</html>

'hope that helps.

--Dave
 

D'oh! I was thinking there was no default action to halt.

Sorry, AvaB. Well spotted, LookingForInfo. Have a star.
 
I see. I noticed the submitting when I was playing around with the code earlier but couldnt understand why it would submit the form. I didnt know this was default for an input type of image. Thanks to both for your input!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top