views:

282

answers:

3

Hi,

How do I add/remove a class from a div when it already has one or more classes?

<div class="class1 class2" id="id1">some text</div>



$("#id1").toggleClass("class3"); // doesn't work


$("#id1").toggleClass(" class3"); // doesn't work

Do I have to parse the string?

A: 

I believe you can iterate through the classes.

Here's a discussion about in on the JQuery google groups.

EDIT: Found a SO question related to this also.

Ólafur Waage
+3  A: 

Your code should definitely work. In fact, the toggleClass example uses multiple class names!

Konrad Rudolph
straing it does work, I swear I was trying to do the same thing last night and it wasn't working!
mrblah
+1  A: 

Have you tried $("#id1").addClass("classname") and $("#id1").removeClass("classname")?

patmortech