Peter
Peter

Reputation: 11825

Jquery - How to trigger $('#myForm').submit(function()

how can i trigger this function without using the form submit?

$('#myForm').submit(function()
{ ....

Upvotes: 22

Views: 68622

Answers (5)

simplyharsh
simplyharsh

Reputation: 36373

You will have to dig it from DOM data. But, let me tell you, its not recommended. Try this,

var form = $('#myForm');
var data = form.data();
var events = data.events;

If the handler function is attached to form 'submit', it will be present as,

var submit_list = events.submit;

Now, if all goes well, at best you can get submit_list as list of all the handler objects attached to submit event of form.

Shortcut: Assuming you have one and only one handler attached to submit event for #myForm,

$('#myForm').data().events.submit[0].handler

is your function.

Play with data(), but remember its not recommended. Happy Coding.

Upvotes: 0

Vinayak Phal
Vinayak Phal

Reputation: 8919

You can directly use something like this

$('#button1').click(function(){
    $('#search').submit();
});

Or you can use it from any javascript code. Which will trigger your $('#myForm').submit(function(){..... function code.

Upvotes: 6

Rafay
Rafay

Reputation: 31033

<input type="button" id="btn" value="click">

jquery

     $(function(){
        $('#myForm').submit(function()
             { ....});

         $("#btn").click(function(){
         $('#myForm').trigger('submit');
        });
     });

Upvotes: 0

ipr101
ipr101

Reputation: 24236

You could try -

$('#myForm').trigger('submit');

Working demo - http://jsfiddle.net/ipr101/KvwMb/1/

Upvotes: 47

NimChimpsky
NimChimpsky

Reputation: 47280

return false;

adding this to function will stop the form submitting.

Or if you want to call function on a different event put the function in an appropriate event handler (a non-submit button's click?)

Upvotes: 2

Related Questions