40

How can I submit a specific form using the follow code (example the id of my form is #form1).

$(".nextbutton").click(function() { submit; });
user342391
  • 7,569
  • 23
  • 66
  • 88

7 Answers7

60

Try this lets say your form id is formID

$(".nextbutton").click(function() { $("form#formID").submit(); });
Pramendra Gupta
  • 14,667
  • 4
  • 33
  • 34
  • 21
    Isn't "form#formID" a bit redundant? "#formID" would be sufficient, right? – Andy Groff Dec 07 '11 at 00:41
  • 18
    Yes, only the ID would suffice. But prepending it with the tagname speeds up the search for the item. If you add it, only
    element will be evaluated until the correct ID is found. Granted, most webpages don't have that much HTML to actually have a delay in searching, but it still is better practice to prepend the tagname if you know it.
    – Flater Jul 11 '12 at 10:09
  • according to the documentation here http://api.jquery.com/submit/ this will add a submit listener on the form, not actually submit it. the submit function is not the same on a jQuery object and on a DOM element. – challet Jan 14 '13 at 16:06
  • This definitely submits the form.... in this case, with id 'formID' – Alex Jan 14 '13 at 16:20
  • 2
    @challet the documentation you link to does list a no-argument form of `submit` that is a shortcut for `.trigger('submit')` and submits the form. – Russell Silva Jun 19 '13 at 20:46
  • you're right, blame on me :) – challet Jun 20 '13 at 22:35
  • @Flater : no, searching for an ID uses the native getElementById which is as fast as an hashtable allows it to be. Prepending a tag name to an ID will only add an useless filtering process. – Ninj May 02 '14 at 15:15
8

You can try like:

   $("#myformid").submit(function(){
        //perform anythng
   });

Or even you can try like

$(".nextbutton").click(function() { 
    $('#form1').submit();
});
GautamD31
  • 28,552
  • 10
  • 64
  • 85
2

Since a jQuery object inherits from an array, and this array contains the selected DOM elements. Saying you're using an id and so the element should be unique within the DOM, you could perform a direct call to submit by doing :

$(".nextbutton").click(function() { 
  $("#formID")[0].submit();
});
challet
  • 870
  • 9
  • 21
2

You don't really need to do it all in jQuery to do that smoothly. Do it like this:

$(".nextbutton").click(function() { 
   document.forms["form1"].submit();
});
Goose
  • 4,764
  • 5
  • 45
  • 84
Alex B.
  • 647
  • 7
  • 21
1

If you have only 1 form in you page, use this. You do not need to know id or name of the form. I just used this code - working:

document.forms[0].submit();
Vova Popov
  • 1,053
  • 11
  • 11
1

Use the following jquery to submit a selectbox with out a submit button. Use "change" instead of click as shown above.

$("selectbox").change(function() { 
   document.forms["form"].submit();
});

Cheers!

Rob Bach
  • 11
  • 1
0

Try this :

$('#form1').submit();

or

document.form1.submit();
whoan
  • 8,143
  • 4
  • 39
  • 48
Ram Ch. Bachkheti
  • 2,609
  • 2
  • 17
  • 14