Saurabh Kumar
Saurabh Kumar

Reputation: 16651

how to add multiple css classes to a html element usin javascript

I want to add multiple classes to an html element

I know the following code is possible.

$("ul#List").find("li.template")
            .removeClass("template")
            .addClass("entry")

But i want to know if the following code is also possible.?

$("ul#List").find("li.template")
            .removeClass("template")
            .addClass("entry1")
                    .addClass("entry2")

Upvotes: 0

Views: 905

Answers (3)

Eric Fortis
Eric Fortis

Reputation: 17350

$("ul#List").find("li.template")
            .removeClass("template")
            .addClass("entry1 entry2")

Edit: Take a look http://jsfiddle.net/nHs9W/

Both of the below snippets work

.addClass("entry1 entry2");

.addClass("entry1").addClass("entry2");

Upvotes: 3

Bojangles
Bojangles

Reputation: 101473

-1 for not reading the manual!

But anyway, from that page,

.addClass( className )
className: One or more class names to be added to the class attribute of each matched element.

The most important part being one or more

Upvotes: 0

J. Ed
J. Ed

Reputation: 6742

you can add multiple classes separated by a space:

.addClass("entry1 entry2")

Upvotes: 2

Related Questions