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:
<!DOCTYPE HTML><html><head><title>How to add class to an element withoutaddClass() method in jQuery ?</title><scriptsrc=</script><style>#el {background: green;}.newClass {color: white;}</style></head><bodystyle="text-align:center;"><h1style="color:green;">GeeksForGeeks</h1><pid="GFG_UP"></p><pid="el">This is the element</p><buttononclick="GFG_Fun()">Click Here</button><pid="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>chevron_rightfilter_none -
Output:
Approach 2: We can also use attr() method and prop() method to add a new attribute ‘class’ to the element.
-
Example:
<!DOCTYPE HTML><html><head><title>How to add class to an element withoutaddClass() method in jQuery ?</title><scriptsrc=</script><style>#el {background: green;}.newClass {color: white;}</style></head><bodystyle="text-align:center;"><h1style="color:green;">GeeksForGeeks</h1><pid="GFG_UP"></p><pid="el">This is the element</p><buttononclick="GFG_Fun()">Click Here</button><pid="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>chevron_rightfilter_none -
Output:
Recommended Posts:
- jQuery UI | addClass() Method
- jQuery | addClass() with Examples
- How to check an element contains a class using jQuery?
- How to get the class of a element which has fired an event using JavaScript/JQuery?
- How to find the parent class name with known class in jQuery ?
- How to remove parent element except its child element using jQuery ?
- How to select a div with a certain class using jQuery ?
- jQuery | .class Selector
- Not class selector in jQuery
- How to select all elements without a given class using jQuery ?
- jQuery | Move an element into another element
- How to create a CSS rule or class at runtime using jQuery ?
- How to fire event if CSS class is changed using jQuery ?
- jQuery | Create a div element
- How to get focused element using jQuery?
- How to select an element by name with jQuery?
- How to get all CSS styles associated with an element using jQuery ?
- jQuery | element + next Selector
- How to create a div element in jQuery ?
- How to get a DOM Element from a jQuery Selector ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


