JQuery Tutorial: Add & Remove A CSS Class From ALL Input Text Fields

One of the things I like most about JQuery is how easy it is to add and remove CSS classes from a form when you’re trying to do input validation. If the user enters an error you can quickly add a red box around the field or perhaps change the background color and text colors to red. Then once the user’s correct the error you can just as easily replace the class with another one. Or if you want to do all of your validation checking on the form submit then you can easily add and remove all of the error classes.

 

To add a CSS class to a single input field:

$(“#field_id_name_here”).addClass(“your_class_name_here”);

 

To add a CSS class to ALL input text fields:

$(“input:text”).addClass(“your_class_name_here”);

 

To remove a CSS class on a single input field:

$(“#field_id_name_here”).removeClass(“your_class_name_here”);

 

To remove a CSS class on ALL input text fields:

$(“input:text”).removeClass(“your_class_name_here”);

You may also like...

Leave a Reply