I have a simple jquery loop that goes through my form and
Basically:
// check all the inputs have a value...
$('input').each(function() {
if($(this).val() == '') {
$(this).addClass('empty');
var error = 1;
}
});
This works a charm. However, as my code continues, I can't seem to access that 'error' variable... as though it is locked inside the each loop. With the following code being right after the .each() loop, I never get my_error_function() to fire, even though I know that criteria 1 and 2 are working.
if(error == 1) {
my_error_function();
} else {
my_non_error_function();
}
How do I access this variable so I can use its result elsewhere in the code?
Define your error variable outside of your function/loop
var error = 0;
$('input').each(function() {
if($(this).val() == '') {
$(this).addClass('empty');
error = 1;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With