submit event handling

J

jman

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.
 
M

Martin Honnen

jman said:
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
);
 
T

Thomas 'PointedEars' Lahn

jman said:
<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
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top