Add Class and Remove Class in jQuery |.addClass()|.removeClass()

Add Class and Remove Class in jQuery |.addClass()|.removeClass()

In this tutorial, you will learn how to add and remove css classes on html elements with onlick using addClass(), removeClass(), toggleClass() method.

How to Add Class and Remove Class in jQuery

There are three types of jQuery methods available to add or remove classes of the selected html elements, you can use these methods to add or remove classes of the html elements using jQuery; as follows:

  • addClass() method
  • removeClass() method
  • toggleClass() method

jQuery addClass() Method

The addClass() method in jQuery is used to add one or more CSS classes to the selected HTML elements. This allows you to dynamically apply styling to elements on a web page without directly modifying the HTML markup.

Syntax

$(selector).addClass();

Example of jQuery addClass() method

You can see that example of the jquery addClass() method, the following example will add class of the selected html elements.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Removing all the Contents using empty() jQuery</title>
<style type="text/css">
.container{
    padding: 15px;
    background: #1EBBA3;
    border: 1px solid #bead18;
}
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
    $("#btn_click").click(function(){
       $(".add_here").addClass("container");
    });
});
</script>
</head>
<body>
    <div class="add_here">
        <h1>Hello World!</h1>
        <p><strong>Note:</strong> If you click the following button it will add the '.container class' where is add_here exist </p>
        <button type="button" id="btn_click">Click Here</button>
    </div>
</body>
</html>                            

Output

Removing all the Contents using empty() jQuery

Hello World!

Note: If you click the following button it will add the ‘.container class’ where is add_here exist

jQuery removeClass() Method

The removeClass() method in jQuery is used to remove one or more CSS classes from the selected elements in a webpage’s DOM (Document Object Model). jQuery is a popular JavaScript library that simplifies many aspects of DOM manipulation and interaction.

Syntax

$(selector).removeClass();

Example of jQuery removeClass() method

You can see that example of the jquery removeClass() method, the following example will remove class of the selected html elements.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Removing all the Contents using empty() jQuery</title>
<style type="text/css">
.container{
    padding: 15px;
    background: #1EBBA3;
    border: 1px solid #bead18;
}
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
    $("#btn_remove").click(function(){
       $(".add_here").removeClass("container");
    });
});
</script>
</head>
<body>
    <div class="add_here container">
        <h1>Hello World!</h1>
        <p><strong>Note:</strong> If you click the following button it will remove the '.container class', where is add_here class exist </p>
        <button type="button" id="btn_remove">Click Here</button>
    </div>
</body>
</html>                            

Output

Removing all the Contents using empty() jQuery

Hello World!

Note: If you click the following button it will remove the ‘.container class’, where is add_here class exist

jQuery toggleClass() Method

Using the jQuery toggleClass()method, you can add or remove one or more classes from the selected html elements. In case if the selected html element already has the class, then it is removed. if an element does not have the class, then it is added.

Syntax

$(selector).toggleClass();

Example of jQuery toggleClass() method

You can see that example of the jquery toggleClass() method, the following example will add remove class of the selected html elements.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Removing all the Contents using empty() jQuery</title>
<style type="text/css">
.container{
    padding: 15px;
    background: #1EBBA3;
    border: 1px solid #bead18;
}
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
    $("#btn_toggle").click(function(){
       $(".add_here").toggleClass("container");
    });
});
</script>
</head>
<body>
    <div class="add_here container">
        <h1>Hello World!</h1>
        <p><strong>Note:</strong> If you click the following button it will add or remove the '.container class', where is add_here class exist </p>
        <button type="button" id="btn_toggle">Click Here</button>
    </div>
</body>
</html>                            

Output

Removing all the Contents using empty() jQuery

Hello World!

Note: If you click the following button it will add or remove the ‘.container class’, where is add_here class exist

Recommended jQuery Tutorial

  1. jQuery Remove Attribute and Disabled Attribute From Element
  2. jQuery | Event MouseUp By Example
  3. Event jQuery Mouseleave By Example
  4. jQuery Event Mouseenter Example
  5. Event jQuery MouseOver & MouseOut By Example
  6. keyup jquery event example
  7. Jquery Click Event Method with E.g.
  8. Event jQuery. Blur By Example
  9. jQuery form submit event with example
  10. keydown function jQuery
  11. List of jQuery Events Handling Methods with examples
  12. Jquery Selector by .class | name | #id | Elements
  13. How to Get the Current Page URL in jQuery
  14. jQuery Ajax Get() Method Example
  15. get radio button checked value jquery by id, name, class
  16. jQuery Set & Get innerWidth & innerHeight Of Html Elements
  17. jQuery Get Data Text, Id, Attribute Value By Example
  18. Set data attribute value jquery
  19. select multiple class in jquery
  20. How to Remove Attribute Of Html Elements In jQuery
  21. How to Checked Unchecked Checkbox Using jQuery
  22. jQuery removeClass & addClass On Button Click By E.g.
  23. To Remove whitespace From String using jQuery
  24. jQuery Ajax Post Method Example
  25. jQuery Ajax Get Method Example
  26. To Load/Render html Page in div Using jQuery Ajax $.load
  27. jQuery Sibling Methods – To Find Siblings Elements
  28. jQuery Find Siblings Elements with Class, id

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *