Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
how to cancel an event's normal behaviour in the traditional model
Old 09-12-2009, 06:42 PM how to cancel an event's normal behaviour in the traditional model
Junior Talker

Posts: 4
Name: Andrew
Trades: 0
using the in-line model I can cancel an event by using the "return" keyword and returning false. For example:

HTML Code:
<input type="submit" onsubmit="return doSomething();" />
and of course the function doSomething would either return true or false.

using the the advanced model I can use the preventDefault method to cancel the event and that is fine for the advanced model but how can I cancel an event using the traditional model such as:

Code:
element.onsubmit = doSomething;
DamianWarS is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-13-2009, 10:57 AM Re: how to cancel an event's normal behaviour in the traditional model
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Same as the inline model. Return false. But you need to do it either from an anonymous function or a function called without parenthesis:
Code:
element.onsubmit = function() {return false;}
function returnFalse() {
    return false;
}
element2.onsubmit = returnFalse;
An example that will not work:
Code:
function returnFalse() {
    return false;
}
element2.onsubmit = returnFalse();
//calling function with () disassociates it from the element
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 09-13-2009, 03:08 PM Re: how to cancel an event's normal behaviour in the traditional model
Junior Talker

Posts: 4
Name: Andrew
Trades: 0
thanks, you're absolutely right. I was just having a moment there. I also put incorrect code in my sample html. you can't call a submit event on an input element. that should have been a form element.

when I use the traditional model to register the event like this example:

element.onsubmit = doSomething;

"element" is the object and "onsubmit" is like a property of the object. "doSomething" is the function. The function doSomething will get executed every time the event in triggerd and if doSomething returns "false" then the onsubmit of the element will equal false for that instance and thus nothing happens. If I put opening and closing parentheses like this:

element.onsubmit = doSomething();

The function is called right away and the onsubmit equals whatever the result is. Every time the event is triggered it will still equal the initial value of whatever was the result of the function and if it equaled false the form will never get submitted.
DamianWarS is offline
Reply With Quote
View Public Profile
 
Old 09-13-2009, 07:36 PM Re: how to cancel an event's normal behaviour in the traditional model
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
Originally Posted by DamianWarS View Post
you can't call a submit event on an input element. that should have been a form element.
Lol, I totally didn't catch that.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 09-13-2009, 07:41 PM Re: how to cancel an event's normal behaviour in the traditional model
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,522
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Code:
try ...

catch ... 

whoops...
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to how to cancel an event's normal behaviour in the traditional model
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23396 seconds with 12 queries