You are here: irt.org | FAQ | JavaScript | Form | 7.1 | Q291 [ previous next ]
Its the opposite question to "How can I submit a form which has more than one text field by just pressing enter?" You can either add a another text field, or, you can trap the user pressing the enter key, rather than clicking the submit button by using a boolean flag:
<SCRIPT LANGUAGE="JavaScript"><!-- var clickedButton = false; //--></SCRIPT> <FORM onSubmit="return clickedButton"> <INPUT TYPE="TEXT"> <INPUT TYPE="SUBMIT" onClick="clickedButton=true"> </FORM>
You might want to reset the boolean falg after the form has been submitted, especially if submitting the form the client side processing:
<SCRIPT LANGUAGE="JavaScript"><!--
var clickedButton = false;
function check() {
if (clickedButton) {
clickedButton = false;
return true;
}
else
return false;
}
//--></SCRIPT>
<FORM onSubmit="return check()">
<INPUT TYPE="TEXT">
<INPUT TYPE="SUBMIT" onClick="clickedButton=true">
</FORM>