I want to get a handle on the form being submitted, before submitting.
- There might be more than one form in the page
- I do not know the form name/id
reason: I want to do some tweeking before the form is being submitted in the template level.
I want to get a handle on the form being submitted, before submitting.
reason: I want to do some tweeking before the form is being submitted in the template level.
With jQuery it'd be something like this:
$(function() {
$('form').submit(function() {
// the code goes here;
// variable `this` is an instance of form
alert($(this).className);
});
});
If you use jQuery, you could look at doing something like this:
$("form").submit(function(e) {
console.log("Form ID that is being submit %s",$(this).attr("id"));
});
In Pure javascript you could so something similar by doing a document.getElementsByTagName("form") and loop through the array you get.
Without jQuery it'd be somthing like this:
for (var i=0; i < document.forms.length; i++){
document.forms[i].onSubmit = function(){
// logic goes here;
// document.forms[i] is the instance of form
if (formIsHappy()){
return true; //form submits
}else{
return false; //prevents the submit
}
};
}