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.

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

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

    function doOnSubmit(e) { = 'loudly';
    alert('doOnSubmit(e) called.');

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


    <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

    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. 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
    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
    Matt Kruse, Jun 29, 2004
    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.