I have a simple jquery loop that goes through my form and
- sees if there are empty fields.
- If they are empty, label them with an “empty” class and
- then create the variable 'error'
Basically:
// check all the inputs have a value... $('input').each(function() { if($(this).val() == '') { $(this).addClass('empty'); var error = 1; } });
It works. However, as my code continues, I cannot access this "error" variable ... as if it was locked inside each loop. When the following code is right after the .each () loop, I never run my_error_function (), although I know that criteria 1 and 2 work.
if(error == 1) { my_error_function(); } else { my_non_error_function(); }
How do I access this variable so that I can use its result elsewhere in the code?
source share