Returning false ignores onSubmit listener

Discussion in 'Javascript' started by Chris, Jun 28, 2004.

  1. Chris

    Chris Guest

    I am wondering about what seems to be a particular quirk in Javascript
    that does not allow form event listeners (e.g. "onsubmit=...") to work
    properly. In the code below, I would like to set up a function
    (doOnSubmit) that is called when the form is submitted.

    But there is a "return false;" in the "onclick" of the button. If this
    "return false" is removed, the code works as I would hope it would.
    With it there, doOnSubmit is never called.

    <html>
    <head>
    <title>A Broken Umbrella</title>
    <script language="Javascript">

    function doOnClick() {
    document.getElementById('form1').input.value = 'quietly';
    }

    function doOnSubmit(e) {
    e.target.input.value = 'loudly';
    alert('doOnSubmit(e) called.');
    }

    function startUp() {
    document.getElementById('form1').onsubmit = doOnSubmit;
    }


    </script>

    </head>
    <body onload="startUp();">
    <form id="form1" method="GET" action="somepage.html">
    <input id="input" name="input" type="text" value="adverbs" /><br />
    <button onclick="doOnClick(); this.submit; return
    false;">Submit</button>
    </form>
    </body>
    </html>

    If I had the option to, I would remove the "return false" from the
    button. However, I am unable to do so and would still like to be able
    to modify the onsubmit event.

    Thanks for any insight you may have.
     
    Chris, Jun 28, 2004
    #1
    1. Advertisements

  2. Chris

    Lee Guest

    Chris said:
    One insight is that it looks like you're trying to bypass some
    sort of validation function in a web page that isn't your own.
    If that's not the case, why can't you remove the "return false"?
     
    Lee, Jun 28, 2004
    #2
    1. Advertisements

  3. Chris

    Matt Kruse Guest

    The onSubmit event doesn't fire if the form is submitted via javascript.
    If you can modify the form object, can't you also just modify the button
    itself?
     
    Matt Kruse, Jun 29, 2004
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.