By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,567 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,567 IT Pros & Developers. It's quick & easy.

submit event handling

P: n/a
i've got an html form object in which i set the onsubmit attribute to
test something and if
it fails i don't want the submit to go thru.

<FORM ... onsubmit="if (fail) return false">

this works.

but if i do it in javascript

formObj.addEventListener( "submit", function()

{
if ( fail)
return false;

return true;
}, false )

this does not work.

can i get it to behave similar to the html code.

that is - if false do not proceed with the submit process.
thanks.
Jul 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
jman wrote:
but if i do it in javascript

formObj.addEventListener( "submit", function()

{
if ( fail)
return false;

return true;
}, false )

this does not work.
Call preventDefault on the event object e.g.
formObj.addEventListener("submit",
function (evt) {
if (fail) {
evt.preventDefault();
}
},
false
);
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 27 '08 #2

P: n/a
jman wrote:
<FORM ... onsubmit="if (fail) return false">

this works.

but if i do it in javascript

formObj.addEventListener( "submit", function()

{
if ( fail)
return false;

return true;
}, false )

this does not work.

can i get it to behave similar to the html code.

that is - if false do not proceed with the submit process.
formObj.addEventListener("submit",
function(e) {
if (fail)
{
e.preventDefault();
}
},
false);

<http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-cancelation>

However, there is no point in using addEventListener() here.

Your Shift key is borken.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jul 27 '08 #3

P: n/a
jman wrote on 27 jul 2008 in comp.lang.javascript:
<FORM ... onsubmit="if (fail) return false">
<form ... onsubmit="return !fail;">
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.