onSubmit not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stuart Wexler

    onSubmit not working

    Hi,

    I have a form with onSubmit embedded in the <form>
    tag. The form is submitted programatically through
    javascript [ie... form.submit()]. While the form submits fine, nothing
    I'm doing seems to get it to also register the code in the onSubmit event.
    Any suggestions as to why this is happening or how to resolve it?

    -Stu


  • Grant Wagner

    #2
    Re: onSubmit not working

    Stuart Wexler wrote:
    [color=blue]
    > Hi,
    >
    > I have a form with onSubmit embedded in the <form>
    > tag. The form is submitted programatically through
    > javascript [ie... form.submit()]. While the form submits fine, nothing
    > I'm doing seems to get it to also register the code in the onSubmit event.
    > Any suggestions as to why this is happening or how to resolve it?
    >
    > -Stu[/color]

    Yes, this is correct behavior. the onsubmit event does not fire if you call
    document.forms['yourForm'].submit();

    The way to get around this is to add the onsubmit validation test as a
    condition before calling submit(). So instead of:

    <a href="#"
    onclick="docume nt.forms['yourForm'].submit();retur n false;">Submit</a>

    you would use:

    <a href="#"
    onclick="
    var f = document.forms['yourForm'];
    if (validate(f)) {
    f.submit();
    }
    return false;
    ">Submit</a>

    Indentation and newlines for readability only, it can all go on a single line,
    or be put in a function and called from the onclick event.

    --
    | Grant Wagner <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *


    * Internet Explorer DOM Reference available at:
    *
    Gain technical skills through documentation and training, earn certifications and connect with the community


    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
    * http://www.mozilla.org/docs/web-deve...upgrade_2.html


    Comment

    • kaeli

      #3
      Re: onSubmit not working

      In article <C_OdnRN5VtHJi4 GiXTWJjg@comcas t.com>, Studawg76@comca st.net
      enlightened us with...[color=blue]
      > Hi,
      >
      > I have a form with onSubmit embedded in the <form>
      > tag. The form is submitted programatically through
      > javascript [ie... form.submit()]. While the form submits fine, nothing
      > I'm doing seems to get it to also register the code in the onSubmit event.
      > Any suggestions as to why this is happening or how to resolve it?
      >[/color]

      For whatever reason, onSubmit won't execute from form.submit(). I had
      that problem, too. I never found out if it was a bug or just one of
      those things that doesn't make any sense.
      You have to call the function that onSubmit calls from the same function
      that calls form.submit().

      -------------------------------------------------
      ~kaeli~
      Black holes were created when God divided by 0.
      Not one shred of evidence supports the notion
      that life is serious.


      -------------------------------------------------

      Comment

      • Jim Ley

        #4
        Re: onSubmit not working

        On Mon, 21 Jul 2003 17:36:06 GMT, Grant Wagner
        <gwagner@agrico reunited.com> wrote:
        [color=blue]
        >Stuart Wexler wrote:
        >[color=green]
        >> Hi,
        >>
        >> I have a form with onSubmit embedded in the <form>
        >> tag. The form is submitted programatically through
        >> javascript [ie... form.submit()]. While the form submits fine, nothing
        >> I'm doing seems to get it to also register the code in the onSubmit event.
        >> Any suggestions as to why this is happening or how to resolve it?
        >>
        >> -Stu[/color]
        >
        >Yes, this is correct behavior. the onsubmit event does not fire if you call
        >document.for ms['yourForm'].submit();[/color]

        It's documented correct in IE, but to standards it's wrong, a lot of
        pages would break right now if the correct spec following was done...

        I'd play safe and always call onsubmit manually, then remove it, then
        submit.

        Jim.
        --
        comp.lang.javas cript FAQ - http://jibbering.com/faq/

        Comment

        • Grant Wagner

          #5
          Re: onSubmit not working

          Jim Ley wrote:
          [color=blue]
          > On Mon, 21 Jul 2003 17:36:06 GMT, Grant Wagner
          > <gwagner@agrico reunited.com> wrote:
          >[color=green]
          > >Stuart Wexler wrote:
          > >[color=darkred]
          > >> Hi,
          > >>
          > >> I have a form with onSubmit embedded in the <form>
          > >> tag. The form is submitted programatically through
          > >> javascript [ie... form.submit()]. While the form submits fine, nothing
          > >> I'm doing seems to get it to also register the code in the onSubmit event.
          > >> Any suggestions as to why this is happening or how to resolve it?
          > >>
          > >> -Stu[/color]
          > >
          > >Yes, this is correct behavior. the onsubmit event does not fire if you call
          > >document.for ms['yourForm'].submit();[/color]
          >
          > It's documented correct in IE, but to standards it's wrong, a lot of
          > pages would break right now if the correct spec following was done...
          >
          > I'd play safe and always call onsubmit manually, then remove it, then
          > submit.
          >
          > Jim.[/color]

          I asked a while back about this and received a reply at the time that seemed to
          indicate onsubmit should not fire when submit() is called, but it's certainly
          possible the person responding was incorrect.

          As for browser support, IE 4+, Netscape 3+ and Opera up to 6 did not call onsubmit
          when submit() was invoked. It seems that Opera 7.x does, which can lead to all
          sorts of nasty surprises if you both manually call the validation routine and have
          it called as part of an onsubmit event.

          So you're right, remove the onsubmit event from the form and always call it
          manually if you are invoking submit() anywhere. Better yet, avoid calling submit()
          directly wherever possible.

          --
          | Grant Wagner <gwagner@agrico reunited.com>

          * Client-side Javascript and Netscape 4 DOM Reference available at:
          *


          * Internet Explorer DOM Reference available at:
          *
          Gain technical skills through documentation and training, earn certifications and connect with the community


          * Netscape 6/7 DOM Reference available at:
          * http://www.mozilla.org/docs/dom/domref/
          * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
          * http://www.mozilla.org/docs/web-deve...upgrade_2.html


          Comment

          Working...