Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to append a css class to an element by javascript?

Tags:

javascript

css

Suppose a HTML element's id is known, so the element can be refereced using:

document.getElementById(element_id);

Does a native Javascript function exist that can be used to append a CSS class to that element?

like image 525
omg Avatar asked May 29 '09 17:05

omg


People also ask

Can we use CSS class in JavaScript?

The class name can be used by JavaScript to manipulate the specified element while CSS uses that class name to style that element. Hence, in this post, we will go through how to modify CSS classes in JavaScript but first let us set the environment by initializing elements in HTML and then styling that element in CSS.

How do I add a class to an element in CSS?

If you want to use a class, use a full stop (.) followed by the class name in a style block. Next, use a bracket called a declaration block that contains the property to stylize the element, such as text color or text size. CSS Classes will help you stylize HTML elements quickly.

How do I add a class to an element in HTML?

To add a class to an element rather than replacing its existing classes, use the += operator instead. Note, it is important to prefix the new classname with space; otherwise, one of the existing classes of an element is lost.


5 Answers

var element = document.getElementById(element_id);
element.className += " " + newClassName;

Voilà. This will work on pretty much every browser ever. The leading space is important, because the className property treats the css classes like a single string, which ought to match the class attribute on HTML elements (where multiple classes must be separated by spaces).

Incidentally, you're going to be better off using a Javascript library like prototype or jQuery, which have methods to do this, as well as functions that can first check if an element already has a class assigned.

In prototype, for instance:

// Prototype automatically checks that the element doesn't already have the class
$(element_id).addClassName(newClassName);

See how much nicer that is?!

like image 78
Kenan Banks Avatar answered Oct 03 '22 03:10

Kenan Banks


Adding class using element's classList property:

element.classList.add('my-class-name');

Removing:

element.classList.remove('my-class-name');
like image 31
ego Avatar answered Sep 30 '22 03:09

ego


classList is a convenient alternative to accessing an element's list of classes.. see http://developer.mozilla.org/en-US/docs/Web/API/Element.classList.

Not supported in IE < 10

like image 40
user3270791 Avatar answered Oct 01 '22 03:10

user3270791


When an element already has a class name defined, its influence on the element is tied to its position in the string of class names. Later classes override earlier ones, if there is a conflict.

Adding a class to an element ought to move the class name to the sharp end of the list, if it exists already.

document.addClass= function(el, css){
    var tem, C= el.className.split(/\s+/), A=[];    
    while(C.length){
        tem= C.shift();
        if(tem && tem!= css) A[A.length]= tem;
    }
    A[A.length]= css;
    return el.className= A.join(' ');   
}
like image 37
kennebec Avatar answered Oct 02 '22 03:10

kennebec


You should be able to set the className property of the element. You could do a += to append it.

like image 40
Tom Hubbard Avatar answered Oct 03 '22 03:10

Tom Hubbard