Returning false ignores onSubmit listener

C

Chris

I am wondering about what seems to be a particular quirk in Javascript
that does not allow form event listeners (e.g. "onsubmit=...") to work
properly. In the code below, I would like to set up a function
(doOnSubmit) that is called when the form is submitted.

But there is a "return false;" in the "onclick" of the button. If this
"return false" is removed, the code works as I would hope it would.
With it there, doOnSubmit is never called.

<html>
<head>
<title>A Broken Umbrella</title>
<script language="Javascript">

function doOnClick() {
document.getElementById('form1').input.value = 'quietly';
}

function doOnSubmit(e) {
e.target.input.value = 'loudly';
alert('doOnSubmit(e) called.');
}

function startUp() {
document.getElementById('form1').onsubmit = doOnSubmit;
}


</script>

</head>
<body onload="startUp();">
<form id="form1" method="GET" action="somepage.html">
<input id="input" name="input" type="text" value="adverbs" /><br />
<button onclick="doOnClick(); this.submit; return
false;">Submit</button>
</form>
</body>
</html>

If I had the option to, I would remove the "return false" from the
button. However, I am unable to do so and would still like to be able
to modify the onsubmit event.

Thanks for any insight you may have.
 
L

Lee

Chris said:
I am wondering about what seems to be a particular quirk in Javascript
that does not allow form event listeners (e.g. "onsubmit=...") to work
properly. In the code below, I would like to set up a function
(doOnSubmit) that is called when the form is submitted.

But there is a "return false;" in the "onclick" of the button. If this
"return false" is removed, the code works as I would hope it would.
With it there, doOnSubmit is never called.

<html>
<head>
<title>A Broken Umbrella</title>
<script language="Javascript">

function doOnClick() {
document.getElementById('form1').input.value = 'quietly';
}

function doOnSubmit(e) {
e.target.input.value = 'loudly';
alert('doOnSubmit(e) called.');
}

function startUp() {
document.getElementById('form1').onsubmit = doOnSubmit;
}


</script>

</head>
<body onload="startUp();">
<form id="form1" method="GET" action="somepage.html">
<input id="input" name="input" type="text" value="adverbs" /><br />
<button onclick="doOnClick(); this.submit; return
false;">Submit</button>
</form>
</body>
</html>

If I had the option to, I would remove the "return false" from the
button. However, I am unable to do so and would still like to be able
to modify the onsubmit event.

Thanks for any insight you may have.

One insight is that it looks like you're trying to bypass some
sort of validation function in a web page that isn't your own.
If that's not the case, why can't you remove the "return false"?
 
M

Matt Kruse

Chris said:
<form id="form1" method="GET" action="somepage.html">
<input id="input" name="input" type="text" value="adverbs" /><br />
<button onclick="doOnClick(); this.submit; return
false;">Submit</button>

The onSubmit event doesn't fire if the form is submitted via javascript.
If you can modify the form object, can't you also just modify the button
itself?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top