0

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")
2
  • Yes, that code will work. Although, as other have pointed out, you can do it with a single call to addClass, separating the classes by a space Commented Jul 16, 2011 at 16:34
  • 3
    Would it not have been quicker to try it out than to ask? Commented Jul 16, 2011 at 16:39

3 Answers 3

3
$("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");

1
  • 1
    It's hard to tell. While it's obvious to me what's changed, @Eric, please give explanations in your answers. Commented Jul 16, 2011 at 16:34
2

you can add multiple classes separated by a space:

.addClass("entry1 entry2")
0

-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

2
  • 2
    I apologise, but I never set out to be rude. This answer gives a solution to your problem, and a comment on the side about not reading the most obvious source of information, and wasting our time on StackOverflow instead. Commented Jul 16, 2011 at 16:45
  • It's not a problem Saurabh :-) Commented Jul 16, 2011 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.