473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

submit event handling

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
3 1136
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matt | last post by:
I tried to figure out how many approaches to submit form data. Here's my attempts. Please advise. Thanks!! 1) html submit button, most commonly used. <form name="formName" action="url"...
4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
2
by: ivanhoe | last post by:
All articles and tutorials I've seen on subject of forms say that if there's name and value for submit button(input tag with type="submit"), they'll get submitted along with the rest of the...
13
by: John Kiernan | last post by:
Hey JavaScript gurus... I'm going to try this again. I haven't gotten as much help as I have advice on style<grin>. I appreciate (having programmed in other languages for quite a while) that...
8
by: horos | last post by:
hey all, Ok, a related question to my previous one on data dumpers for postscript. In the process of putting a form together, I'm using a lot of placeholder variables that I really don't care...
3
by: Dabbler | last post by:
How do I tell wether I'm seeing autopostback vs submit button click in Page_load? Thanks for any clues.
6
by: stellstarin | last post by:
I have a HTML page containing two submit buttons in the same form.When the form is submitted,I want to know through which submit button the form was submitted. Is there any event or property which...
4
by: Daniel Smedegaard Buus | last post by:
Hey :) I'm trying to figure this one out, and it's a bit tricky. I have a "tip a friend" form that's submitted via AJAX. When the user presses enter inside one of the form's input fields, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.