182

I'm trying to add and remove multiple classes on a text field by clicking different radio buttons. I'm not able to remove the unwanted classes while switching between different radio buttons.

My code for this is:

// For 1st radio button if (actionUrl == "search-client-by-id") { $("#req").removeClass("validate[required,custom[onlyLetterNumber],maxSize[20],custom[onlyLetterSp]]") .addClass("validate[required,custom[onlyNumberSp]]"); } // For 2nd radio button else if (actionUrl == "search-client-by-name") { $("#req").removeClass("validate[required,custom[onlyNumberSp]]") .addClass("validate[required,custom[onlyLetterNumber],maxSize[20],custom[onlyLetterSp]]"); } 
0

5 Answers 5

355

You can separate multiple classes with the space:

$("p").addClass("myClass yourClass"); 

http://api.jquery.com/addClass/

Sign up to request clarification or add additional context in comments.

2 Comments

I have found a solution for this, every time I use .removeClass() only and no need to write any class name in removeClass to remove all the added classes and add any class by choice. Yes its working...!
Using .removeClass() [with no parameters] removes all classes of the jQuery object.
25

Add multiple classes:

$("p").addClass("class1 class2 class3"); 

or in cascade:

$("p").addClass("class1").addClass("class2").addClass("class3"); 

Very similar also to remove more classes:

$("p").removeClass("class1 class2 class3"); 

or in cascade:

$("p").removeClass("class1").removeClass("class2").removeClass("class3"); 

2 Comments

so what is you want to remove a class from multiple $("p")?
in my example I refer to all the paragraphs on the page but it is just an example. You can identify a specific item with an ID or another class.
3

TL;DR

$("p").removeClass('one four').addClass("three one"); // FYI: Order of addClass & removeClass can be changed too & chaining is also possible, e.g $("p").addClass("three one").removeClass('one four').addClass('class1 class2 class3');` 

More Working examples & details.

// handle button click & add/remove classes $("#myBtn").click(function() { $("#myPara").addClass("three four").removeClass('two three'); // again show the classes applied after we addede/remove multiple classes $("#container").text($("#myPara").attr("class")); });
.one { color: red; } .two { text-decoration: underline; } .three { font-style: bold; } .four { font-style: italic; }
<p id="myPara" class="one two three">hello world</p> <p> Classes applied: <b><span id='container'>one two three</span></b></p> <button id="myBtn">Click Me</button> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Comments

0

use this $("p").addClass("class1 class2 class3");

1 Comment

This has already been mentioned in the other answers. When answering older questions that already have answers, please make sure you provide either a novel solution or a significantly better explanation than existing answers.
-1

easiest way to append class name using javascript. It can be useful when .siblings() are misbehaving.

document.getElementById('myId').className += ' active'; 

2 Comments

This answer is not relevant to the question, which asked explicitly for jQuery.
And you should use classList anyway

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.