Question

A form named form1 has a submit button named button1.

What is difference between $("#form1").submit() and $("#button1").click()?

No correct solution

OTHER TIPS

If you check your html code you will see something like this <form .... action="abcd.html">. The action tag may or may not be a html page, it could be a .php, .aspx or just an url. $("#form1").submit() will post the data to this page/url and then that page will process the data.

$("#form1").submit() will post the data of the form to the url in the action tag of the form while $("#button1").click() will just trigger the click event.

In you case there is no difference.

The submit event is sent to an element when the user is attempting to submit a form. It can only be attached to <form> elements. Forms can be submitted either by clicking an explicit <input type="submit">, <input type="image">, or <button type="submit">, or by pressing Enter when certain form elements have focus.

From .submit() docs.

Using $("#form1").submit() you be able to submit form without submit button on the page.

$("#form1").submit() submits the entire form to a Servlet or anything where as $("#button1").click() simply can be used to process anything like calling a function of javascript or even to submit the form.

$("#form1").submit() just sends data to the server but $("#button1").click() fires button's click event and doesn't send any data to the server.

PS: if the button type is submit, it sends data to the server immediately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top