PositiveGuy
PositiveGuy

Reputation: 47733

How to change a class's content using jQuery

Here is the div element on my page:

How can I change a class's content?

So from existing which is: class="jcarousel-next jcarousel-next-horizontal"

to class="jcarousel-next jcarousel-next-horizontal jcarousel-next- disabled jcarousel-next-disabled-horizontal"

Do I need to completely remove it by using removeClass and then re-add?

Upvotes: 2

Views: 1019

Answers (3)

hegemon
hegemon

Reputation: 6764

Try this:

jQuery('#myobj').removeClass('class-to-remove').addClass('class-to-add');

EDIT: multiple classes at once:

jQuery('#myobj').removeClass('class1 class2').addClass('class3 class4');

Also you can toggle classes with toggleClass().

Brg.

Upvotes: 0

howardr
howardr

Reputation: 333

no you can just use addClass. if you add a class that is already there jQuery wont add it again

$('#myelem').addClass('jcarousel-next-disabled jcarousel-next-disabled-horizontal');

Upvotes: 3

Andrew Hare
Andrew Hare

Reputation: 351476

The addClass function preserves existing classes.

Upvotes: 4

Related Questions