jQuery CSS Classes

Using the jQuery CSS classes, it is easy to manipulate the CSS of elements. jQuery has several methods for CSS manipulation. These are as follows:
addClass()
removeClass()
toggleClass()
css()

jQuery addClass() Method

jQuery addClass() method is used to add class attributes to different elements. This method is only used to add the class name to the class elements not to remove the attributes of the existing class. Syntax:
$(selector).addClass(classname,function(index,currentclass))
classname: It is used to specify one or more class names which you want to add and it is a mandatory parameter. function: It is used to specify a function that returns one or more class names to be added and it is an optional parameter. index: It is used to return the index position of the content. currentclass: It is used to provide the current class name of the selected content.

Example:

<!DOCTYPE html>    
<html>    
<head>    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>    
<script>    
$(document).ready(function(){    
    $("button").click(function(){   
        $("p").addClass("intro");    
    });    
});    
</script>    
<style>    
.intro {    
    font-size: 200%;    
    color: red;    
}    
</style>    
</head>    
<body>      
<h1>This is the first heading</h1>     
<p>Hello, world!.</p>    
<p>This is a paragraph.</p>     
<button>Add a class name to the first p element</button>     
</body>    
</html>
Try Now

jQuery removeClass() Method

jQuery remove() method is used to remove one or more classes from the selected elements. Syntax:
$(selector).removeClass(classname,function(index,currentclass))
classname: It is used to specify one or more class names which you want to remove and it is a mandatory parameter. function: It is used to specify a function that returns one or more class names to be removed and it is an optional parameter. index: It is used to return the index position of the content. currentclass: It is used to provide the current class name of the selected content.

Example:

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
<script>  
$(document).ready(function(){  
    $("button").click(function(){  
        $("h2, p").removeClass("blue");  
    });  
});  
</script>  
<style>  
.important {  
    font-weight: bold;  
    font-size: xx-large;  
}    
.blue {  
    color: blue;  
}  
</style>  
</head>  
<body>   
<h2 class="blue">This is a heading</h2>  
<p class="blue">Hello, World!.</p>   
<p>This is the a paragraph.</p>  
<button>Remove class from elements</button>    
</body>  
</html>
Try Now

jQuery toggleClass()

Using the jQuery toggleClass() method you can add or remove the classes from the selected elements. In this method, if the name of the class is missing, it adds or if the class name is already set, it removes. Syntax
$(selector).toggleClass(classname,function(index,currentclass),switch)
classname: It is used to specify one or more class names which you want to add or remove. It is a mandatory parameter. function: It is used to specify a function that returns one or more class names to be added or removed. It is an optional parameter. index: It is used to return the index position of the content. currentclass: It is used to provide the current class name of the selected content.

Example:

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
<script>  
$(document).ready(function(){  
    $("button").click(function(){  
        $("h1, h2, p").toggleClass("red");  
    });  
});  
</script>  
<style>  
.red {  
    color: red;  
}  
</style>  
</head>  
<body>    
<h1>Heading 1</h1>  
<h2>Heading 2</h2>    
<p>This is a paragraph.</p>  
<p>This is another paragraph.</p>  
<button>Toggle class</button>    
</body>  
</html>
Try Now 

Related Topics

jQuery Events

jQuery event is used to show a moment when something happens. jQuery is used to set up event-driven responses on the HTML page elements. These events have often triggered by the...

1 minute read.

jQuery Add

Using the jQuery, it is easy to add new content. Here are four jQuery methods to add new content: append() prepend() after() before() jQuery append() Method The jQuery append() method is used to insert the content...

4 minutes read.

jQuery Example

Hello, world! Example Here is the jQuery first example: <div id="divTest1"></div>   <script type="type/javascript">   $("#divTest1").text("Hello, world!");   </script> The above code have a div tag with the id of "divTest1" and $ shortcut to access jQuery. In this code select all...

2 minutes read.

jQuery Get()/Post() Methods

The jQuery get() and post() methods is used to send a HTTP request to a page and get the result back from the web server. GET and POST are the two...

3 minutes read.

jQuery Form Events

When a form control receives or loses focus or when form control value is modified by the user such as by typing text in a text input, select any option...

3 minutes read.

jQuery Mouse Events

When the users move the mouse pointer or click some element then the mouse event fires. To handle the mouse events here are some jQuery methods: 1) The click() Method An event...

2 minutes read.

jQuery Remove

Using the jQuery, it is easy to remove the existing HTML content. There are two jQuery methods to remove the content: remove() empty() jQuery remove() Method The jQuery remove() method is used to remove the...

2 minutes read.

jQuery Syntax

jQuery syntax is made by using HTML elements. You can perform some action on those elements. jQuery starts with the dollar sign ($) and ends with a semicolon (;). Basic syntax...

1 minute read.

jQuery Document/Window Events

When the user resizes or scrolls the browser window then the events triggers. Here are some jQuery methods to handle the document/ window events: 1) The ready() Method JQuery ready() method is...

2 minutes read.

jQuery Animation Effect

The jQuery animate() method is a way to create custom animations. The jQuery animate() method is used to animate numeric properties of CSS. Syntax: $(selector).animate({params}, speed, callback); speed: Speed is an optional parameter. It is used...

1 minute read.

jQuery Sliding Effect

jQuery slide is used to slides the element out of the viewport. Here are some slide methods: slideDown() slideUp() slideToggle() jQuery slideDown() slideDown() method of jQuery is used to slide down an element. Syntax: ...

3 minutes read.

jQuery CSS Classes

Using the jQuery CSS classes, it is easy to manipulate the CSS of elements. jQuery has several methods for CSS manipulation. These are as follows: addClass() removeClass() toggleClass() css() jQuery addClass() Method jQuery addClass() method is used to...

3 minutes read.

jQuery Keyboard Events

When the user press or release a key on the keyboard then the keyboard event fires. Here are some jQuery methods to handle the keyboard events. 1) The keypress() Method: When a keyboard...

2 minutes read.

jQuery load() Method

jQuery load() method is a powerful AJAX method used for loading data asynchronously. The load() method loads HTML or text content from a server and added into a DOM element. Syntax: $(selector).load(URL,data,callback); Here is...

2 minutes read.

jQuery Selectors

When you use the JavaScript then read and modify the content of the page is the very common task. While doing this you want to find the elements that you...

3 minutes read.

jQuery each() method

jQuery each() method: jQuery contains many processes that make coding a much better and faster work for professionals.  The each() method is one of the earliest method of jQuery. It...

3 minutes read.

jQuery CSS() Method

Using the jQuery css() method, you can set or return the one or more style properties for the selected elements. Here are the two ways to provide jQuery css() method: Set a...

2 minutes read.

jQuery Tutorial

jQuery tutorial provides deep knowledge of jQuery technology for both beginners and professionals. In this jQuery tutorial you will learn about example, selectors, traversing, events, effects, jQuery fundamentals, CSS and attributes....

3 minutes read.

jQuery hide/show Effect

jQuery provides a interface for doing various kind of amazing effects like Slide, Fade, Hide, Show, Toggle Effects and Animate. jQuery hide() and show() Using the jQuery show() and hide() methods, you...

2 minutes read.

jQuery Fading Effect

jQuery Fade Using jQuery, you can fade elemnts in and out of visibility. You can change the opacity level of elements from invisible state to visible state or visible state to...

6 minutes read.