I have a form that I would like all fields to be filled in. If a field is clicked into and then not filled out, I would like to display a red background.
Here is my code:
$('#apply-form input').blur(function () {
if ($('input:text').is(":empty")) {
$(this).parents('p').addClass('warning');
}
});
It applies the warning class regardless of the field being filled in or not.
What am I doing wrong?
You can try something like this:
It will apply
.blur()
event only to the inputs with empty values.There is one other thing you might want to think about, Currently it can only add the warning class if it is empty, how about removing the class again when the form is not empty anymore.
like this:
Check if all fields are empty and append ON or OFF on Filter_button
With HTML 5 we can use a new feature "required" the just add it to the tag which you want to be required like:
<input type='text' required>