0

I have written a function on submit of a form like this:

$("#form").submit(function(){
    // do something
});

And form is submitted like this:

document.formName.submit();

It doesn't trigger my function. If I wrap form in jQuery and then submit it, it works fine:

$(document.formName).submit();

I was thinking binding events to jQuery DOM object will trigger the events on raw DOM object as well, but it doesn't happen in this case.

Example:

http://jsfiddle.net/aZasZ/1/

3 Answers 3

1
document.formName.submit();

does not trigger the event as it is the actual DOM Element and does not fire off any events,

while

$(document.formName).submit();

is a trigger function for firing of events within JQuery.

i would suggest you use the

$('form').submit(); 

to submit the form or

$('form').triggerHandler('submit') 

in the case of you wanting to fire only that function handler.

Sign up to request clarification or add additional context in comments.

Comments

0

You probably just have gotten your IDs wrong.

Here's a fiddle demonstrating the behavior, feel free to use that.

http://jsfiddle.net/aZasZ/

(I just added $("#form").submit() )

3 Comments

you didn't understand me. I am submitting form using document.formName. It doesn't work. Look at this: jsfiddle.net/aZasZ/1
@emphaticsunshine: read Qpirate's answer. He's telling you why!
@emphaticsunshine: have a look at my updated answer. Maybe it can help.
0

Have you tried

$("form#myForm").submit();

and this is the whole code:

    <form id="MyForm" method="POST" action="">
        Field1:<input type="text" id="field1" value="" />
        Field2:<input type="text" id="field2" value="" />
        <input type="submit" value="send" />
    </form>

    <button id="ShowLink">Vai</button>
    <script type="text/javascript">
        $(document).ready(function () {
            $("form#MyForm").submit( function () {
              alert("Hello!");
              return(true);
            } );
        })
    </script>

UPDATE:

To achieve what you're trying to do you can overwrite the submit function. See this fiddle.
Credits to this answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.