How to add class to an element without addClass() method in jQuery ? Last Updated : 26 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The task is to add a new class to the element without using addClass() method with the help of jQuery. There are two approaches that are discussed below: Approach 1: To perform the operation, we can use toggleClass() method to toggle the class which we want to add to the element. Example: HTML <!DOCTYPE HTML> <html> <head> <title> How to add class to an element without addClass() method in jQuery ? </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> <style> #el { background: green; } .newClass { color: white; } </style> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP"></p> <p id="el">This is the element</p> <button onclick="GFG_Fun()"> Click Here </button> <p id="GFG_DOWN"></p> <script> var el_up = document.getElementById('GFG_UP'); var el_down = document.getElementById('GFG_DOWN'); el_up.innerHTML = "Click on the button to " + "perform the operation."; function GFG_Fun() { $('#el').toggleClass('newClass'); el_down.innerHTML = "Class has been added"; } </script> </body> </html> Output:Approach 2: We can also use attr() method and prop() method to add a new attribute 'class' to the element. Example: HTML <!DOCTYPE HTML> <html> <head> <title> How to add class to an element without addClass() method in jQuery ? </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> <style> #el { background: green; } .newClass { color: white; } </style> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP"></p> <p id="el">This is the element</p> <button onclick="GFG_Fun()"> Click Here </button> <p id="GFG_DOWN"></p> <script> var el_up = document.getElementById('GFG_UP'); var el_down = document.getElementById('GFG_DOWN'); el_up.innerHTML = "Click on the button to " + "perform the operation."; function GFG_Fun() { $('#el').attr('class', 'newClass'); el_down.innerHTML = "Class has been added"; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to add class to an element without addClass() method in jQuery ? P PranchalKatiyar Follow Improve Article Tags : Web Technologies JQuery jQuery-Questions Similar Reads How to add and remove CSS classes to an element using jQuery ? In this article, we will see how to add or remove the CSS classes to an element using jQuery. To add the CSS classes to an element we use addClass() method, and to remove the CSS classes we use removeClass() method. Syntax: The syntax for adding CSS classes to an element: $('selector').addClass(clas 2 min read How to add a new class to an element that already has a class using jQuery ? In this article, we will learn to add a new class to an element that already has a class using jQuery. There are different methods of achieving this task using the built-in methods available in jQuery. The approaches we can use to add a class to an element using jQuery are listed below:Table of Cont 3 min read How to Select All Elements without a Given Class using jQuery? In jQuery, selecting elements that do not have a specific class can be useful when you want to manipulate or style all elements except those matching a certain condition. This approach allows for more precise control over the elements in various dynamic situations.Here we have some common approaches 2 min read How to add a class to the last paragraph element using jQuery ? In this article, we will learn to add a class to the last paragraph element using jQuery. To add a class to the last paragraph element, we use last() and addClass() methods. The jQuery last() function is an inbuilt function that is used to find the last element of the specified elements. Syntax: $(s 1 min read How to attach a method to HTML element event using jQuery ? In this article, we are going to learn, how can we attach a method to an HTML element event using jQuery.There are various events like click(), mouseenter(), mouseout(), etc which are available in jQuery. For attaching a method to an HTML elements event, we will need a jQuery method on(), which will 2 min read Like