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?
And you don’t necessarily need
.lengthor see if it’s>0since an empty string evaluates to false anyway but if you’d like to for readability purposes:If you’re sure it will always operate on a textfield element then you can just use
this.value.Also you should take note that
$('input:text')grabs multiple elements, specify a context or use thethiskeyword if you just want a reference to a lone element (provided there’s one textfield in the context’s descendants/children).