40

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

$(".nextbutton").click(function() { submit; }); 
0

7 Answers 7

60

Try this lets say your form id is formID

$(".nextbutton").click(function() { $("form#formID").submit(); }); 
Sign up to request clarification or add additional context in comments.

7 Comments

Isn't "form#formID" a bit redundant? "#formID" would be sufficient, right?
Yes, only the ID would suffice. But prepending it with the tagname speeds up the search for the item. If you add it, only <form> 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.
according to the documentation here 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.
This definitely submits the form.... in this case, with id 'formID'
@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.
|
8

You can try like:

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

Or even you can try like

$(".nextbutton").click(function() { $('#form1').submit(); }); 

2 Comments

according to api.jquery.com/submit , this will only add an event listener
@challet: According to an example lower down in the page you linked to, it does submit the form.
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(); }); 

Comments

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(); }); 

7 Comments

funny, cause you use jquery in your answer that says you don't need jquery?
I guess he meant the part of submitting the form :)
.submit() doesnt work for me. says property submit is not a function
doesn't work. says "submit is not a function"
This is true but if you've got jQuery available as a tool for selecting DOM elements, you might as well be consistent. Otherwise you're introducing redundancy.
|
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(); 

Comments

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!

Comments

0

Try this :

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

or

document.form1.submit(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.