How can I check for empty values of (required
) input fields within a section, and then add a class to them on an event, using jQuery? So far, I have tried:
jQuery("#sender_container input.required").val("").addClass("error");
But that seems to SET the value, rather than checking it. Any ideas?
If you only need to select based on the initial attribute value of the input then the following will do:
But be aware that this won't work if the
value
attribute isn't present. It also won't work for the current input value if it has been changed by user or script.If you'd like to get the current input value you can use jquery's
filter
function:After you've selected the jquery elements you can add your class:
For those input.required is not working write as below:
DEMO
this simple way may work.
You can try this:
DEMO
Note: This answer should not be used, and the only reason it wasn't deleted is so it can be learned from.