Results 1 to 5 of 5

Thread: Preventing / removing the standard event form a submit button

  1. #1
    Join Date
    Aug 2007
    Location
    Germany
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Preventing / removing the standard event form a submit button

    Hi,

    I'm a newbie to DOM scripting and have run into a problem that was not able to solve with all my books and online sourcces :-(

    So I hope someone could explain how to remove the standard event from a submit button, I mean how can I prevent the submit button from actually submitting something?

    Here's what I've tried so far (simplified):

    First I attach a new event to the submit button (using the "addEvent" - cross-browser workaround from Scott Andrew):

    function submitHandler () {
    submitBtn = document.getElementsByName(submitButtonName)[0];
    addEvent(submitBtn,"click",alertbox,false);


    Then I handle the click on the submit button with my function

    function alertbox () {
    alert("This is not allowed");
    return false;
    }


    The code works just fine until the alert box pops up. But then after clicking "OK" in the alert box the form will still be submitted (what it should not) - but only in Firefox. IE7 does not submit the form and halts after the alert box, just how it's supposed to be.

    I've been playing around with stopPropagation, preventDefault and a lot of functions that I found online, but I just can't get it to work - I just got the feeling that this is some kind of secret science. And I'm afraid I don't even know where do I have to put event removing part - I guess somewhere in the alertbox() function... ???

    Can anyone enligten me on this, any tiny hint would be much appreciated!

    Thanks a lot,
    sdevic

  2. #2
    Join Date
    Aug 2007
    Location
    Germany
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Solved!
    I found a function that stops bubbling and prevents the standard event for all browsers.

    Here it is:
    function stopDefault(e){
    if (window.event && window.event.cancelBubble
    && window.event.returnValue){
    window.event.cancelBubble = true;
    window.event.returnValue = false;
    return;
    }
    if (e && e.stopPropagation && e.preventDefault){
    e.stopPropagation();
    e.preventDefault();
    }
    }


    This function needs to be called from within the event handling function then. Works great.

    Thank you anyway ;-)

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    instead of using a submit type input, use a button?

  4. #4
    Join Date
    Aug 2007
    Location
    Germany
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    of using a submit type input, use a button?
    Unfortunately that's not possible because most forms utilize the standard submit button and the script has to be as generic as possible

  5. #5
    Join Date
    Jan 2008
    Location
    Cincinnati
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Code:
    <input type="button" value="bla" onclick="alertstuff">
    That wont submit the form. Than you could use javascript to submit the form when you want it to.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •