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

Leave a comment Standard

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”);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s