Form onsubmit

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

    Form onsubmit

    If you have an onsubmit event handler defined for a form, it doesn't seem to
    trigger if you manually call the .submit() method of the form object. Is
    there any way you can make sure that code is executed?

    thnx,
    Christoph


  • RobG

    #2
    Re: Form onsubmit

    On Mar 14, 10:15 am, "Sorrow" <jcbo...@yahoo. comwrote:
    If you have an onsubmit event handler defined for a form, it doesn't seem to
    trigger if you manually call the .submit() method of the form object.
    Most browsers work that way, but some will fire the submit event when
    the form's submit method is called programatically (e.g. Opera 7,
    perhaps later versions too, I can't test it right now).

    Is
    there any way you can make sure that code is executed?
    Call it with the function that calls submit. You will need to deal
    with stopping the onsubmit handler from firing twice in some browsers
    - perhaps if it runs successfully, you can remove it before calling
    submit().


    <script type="text/javascript">

    function doSubmit(el) {
    var form = el.form;
    if (typeof form.onsubmit == 'function') {
    if (form.onsubmit( ) !== false) {
    form.submit();
    }
    }
    }

    </script>

    <form onsubmit="alert ('hey'); return false;">
    <div><input type="button" value="submit onclick"
    onclick="doSubm it(this);"></div>
    </form>



    --
    Rob

    Comment

    • SAM

      #3
      Re: Form onsubmit

      RobG a écrit :
      >
      Call it with the function that calls submit. You will need to deal
      with stopping the onsubmit handler from firing twice in some browsers
      - perhaps if it runs successfully, you can remove it before calling
      submit().
      >
      >
      <script type="text/javascript">
      >
      function doSubmit(el) {
      var form = el.form;
      if (typeof form.onsubmit == 'function') {
      if (form.onsubmit( ) !== false) {
      I do not understand at all why :
      form.submit();
      now runs as waited ...

      or is it the form.onsubmit() that fires ?
      }
      }
      }
      Ha ! it is :

      function doSubmit(el) {
      var form = el.form;
      form.onsubmit() ;
      }

      that does the job (in my Fx)
      >
      </script>
      >
      <form onsubmit="alert ('hey'); return false;">
      <div><input type="button" value="submit onclick"
      onclick="doSubm it(this);"></div>
      </form>
      --
      sm

      Comment

      Working...