How do I add a class to a given element?

How do I add a class to a given element?

I have an element that already has a class:

Now I want to create a JavaScript function that will add a class to the div (not replace, but add).
How can I do that?


Solution 1:

Add a space plus the name of your new class to the className property of the element. First, put an id on the element so you can easily get a reference.

<div id="div1" class="someclass">
    <img ... id="image1" name="image1" />


var d = document.getElementById("div1");
d.className += " otherclass";

See also element.className on MDN.

Solution 2:

The easiest way to do this without any framework is to use element.classList.add method.

var element = document.getElementById("div1");

And if you want to remove class from an element –


I prefer not having to add any empty space and duplicate entry handling myself (which is required when using the document.className approach). There are some browser limitations, but you can work around them using polyfills.

Solution 3:

find your target element “d” however you wish and then:

d.className += ' additionalClass'; //note the space

you can wrap that in cleverer ways to check pre-existence, and check for space requirements etc..

Solution 4:

Add Class

  • Cross Compatible

    In the following example we add a classname to the <body> element. This is IE-8 compatible.

    var a = document.body;
    a.classList ? a.classList.add('classname') : a.className += ' classname';

    This is shorthand for the following..

    var a = document.body;
    if (a.classList) {
    } else {
        a.className += ' wait';

  • Performance

    If your more concerned with performance over cross-compatibility you can shorten it to the following which is 4% faster.

    var z = document.body;

  • Convenience

    Alternatively you could use jQuery but the resulting performance is significantly slower. 94% slower according to jsPerf


Removing the class

  • Performance

    Using jQuery selectively is the best method for removing a class if your concerned with performance

    var a = document.body, c = ' classname';

  • Without jQuery it’s 32% slower

    var a = document.body, c = ' classname';
    a.className = a.className.replace( c, '' );
    a.className = a.className + c;


  1. jsPerf Test Case: Adding a Class
  2. jsPerf Test Case: Removing a Class

Using Prototype


Using YUI


Solution 5:

Another approach to add the class to element using pure JavaScript

For adding class:


For removing class:


Solution 6:

When the work I’m doing doesn’t warrant using a library, I use these two functions:

function addClass( classname, element ) {
    var cn = element.className;
    //test for existance
    if( cn.indexOf( classname ) != -1 ) {
    //add a space if the element already has class
    if( cn != '' ) {
        classname = ' '+classname;
    element.className = cn+classname;

function removeClass( classname, element ) {
    var cn = element.className;
    var rxp = new RegExp( "\\s?\\b"+classname+"\\b", "g" );
    cn = cn.replace( rxp, '' );
    element.className = cn;