How to Submit a Form with jQuery
To submit a form with jQuery, use the built-in submit()
method. This is useful when the button to submit the form has the type
attribute set to button
rather than submit
.
Let's create an example HTML form with some inputs a post action and a button with the type
attribute of button
then handle the submit button click using jQuery.
<form action="something" method="post" id="contact">
<label for="name">Name</label>
<input type="text" name="name">
<button type="button" id="submit_contact">Submit</button>
</form>
$('#submit_contact').click(function() {
let form = $(this).parents('form:first');
form.submit();
});
In the above example, we are calling a function when the #submit_contact
button is clicked. Inside the function, we get the parent form and store it in a variable before submitting the form using the submit()
method.
Conclusion
You now know how to perform an HTML form submission using jQuery if you are not using a regular submit button.