How can I set an element to have multiple classes?
Initial attempt:
element.setAttribute("class","class1","class2");
element.className="class1 , class2";
element.class="class1 , class2";
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just set the attribute as normal. It just sets the attribute to whatever string you pass it, it isn’t aware of any special rules for how the value gets handled.
The attribute takes a space separated list of classes so:
However… older versions (I think 7 and lower) of Internet Explorer have serious bugs in their
setAttributeimplementation — so don’t use it. Use theclassNameproperty instead.Note, also, that these are HTML classes. They uses beyond the application of styles. There is no such thing as a CSS class (although there are class selectors, other selectors, rule sets and properties, all of which have been (incorrectly and confusingly) called “classes” at some time or another).