Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  175] [ 1]  / answers: 1 / hits: 118031  / 14 Years ago, mon, august 2, 2010, 12:00:00

I want an HTML form to do nothing after it has been submitted.


action=""

is no good because it causes the page to reload.


Basically, I want an Ajax function to be called whenever a button is pressed or someone hits Enter after typing the data. Yes, I could drop the form tag and add just call the function from the button's onclick event, but I also want the "hitting enter" functionality without getting all hackish.


More From » html

 Answers
106

By using return false; in the JavaScript code that you call from the submit button, you can stop the form from submitting.


Basically, you need the following HTML:


<form onsubmit="myFunction(); return false;">
<input type="submit" value="Submit">
</form>

Then the supporting JavaScript code:


<script language="javascript"><!--
function myFunction() {
// Do stuff
}
//--></script>

If you desire, you can also have certain conditions allow the script to submit the form:


<form onSubmit="return myFunction();">
<input type="submit" value="Submit">
</form>

Paired with:


<script language="JavaScript"><!--
function myFunction() {
// Do stuff
if (condition)
return true;

return false;
}
//--></script>

[#96056] Thursday, July 29, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ravenl

Total Points: 338
Total Questions: 107
Total Answers: 112

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
;