How to check with jQuery if any form is submitted?
As a general answer, to classify a group of elements, use the class
attribute, giving each element the same class name. You can then select elements by that class. An element can have more than one class.
$(".something");
If you must use id, you can use an attribute selector:
$("[id^='something']"); // all elements with an id beginning with 'something'
$("[id$='something']"); // all elements with an id ending with 'something'
For this specific question, since you want to act on any form within the page, the simplest choice would be to use the element name selector:
$("form");
Regardless of the selector, you can identify which form was submitted by referencing this
within the submit()
handler:
$("form").submit(function (e) {
e.preventDefault();
var formId = this.id; // "this" is a reference to the submitted form
});
You can catch all forms by using the selector 'form', e.g.
$("form").submit(function() {
var theForm = $(this);
var formID = theForm.attr("id");
// do something
});