I'm trying to execute a function that is bound to a form which stops the browser from submitting the form and checks that form for missing fields, and if there aren't any, it proceeds to submit the form via Ajax
The issue is, the page contains many very similar forms with identical fields, only with unique numbers after the ID for each form element. I want to pass that number to the function that is bound to the relevant form, however when I initialise any variable other than the 'event' variable, the form submits and ignores event.preventDefault(); AND/OR return false;
Here's a simple test:
Here's the code:
JS:
$('#contact-form1').submit( submitForm("1") );
function submitForm(formId,event) {
var contactForm = $(this);
var forename = '#contact-forename' + formId;
var surname = '#contact-surname' + formId;
var email = '#contact-email' + formId;
var tel = '#contact-tel' + formId;
if ( !$(forename).val() || !$(surname).val() || !$(email).val() || !$(tel).val() ) {
$('.form-status').removeClass("current-status");
$('.contact-incomplete').addClass("current-status");
} else {
$('.form-status').removeClass("current-status");
$('.contact-sending').addClass("current-status");
$.ajax( {
url: contactForm.attr( 'action' ) + "?ajax=true",
type: contactForm.attr( 'method' ),
data: contactForm.serialize(),
success: submitFinished
} );
}
return false;
}
It seems like the problem lies in the attempt to pass a variable to the function.