Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove class attribute from div?

Tags:

javascript

I am using JavaScript and I want to add/remove a Class attribute if a button is clicked. I am able to add the class, but I don't know how to remove it. how can I do that?

window.onload = function(){
    var buttonGo = document.getElementsByTagName('button')[0];
    var buttonCom = document.getElementsByTagName('button')[1];
    var box = document.getElementById('box');
    buttonGo.onclick = function(){
        box.setAttribute('class','move');
    }
    buttonCom.onclick = function(){
        // how to remove class name?
    }
}
like image 749
3gwebtrain Avatar asked Mar 02 '11 14:03

3gwebtrain


People also ask

How do I remove a class from a div element?

To remove a class from an element, you use the remove() method of the classList property of the element.

How do I delete a class attribute?

The removeAttribute() method removes an attribute, and does not have a return value. The removeAttributeNode() method removes an Attr object, and returns the removed object. The result will be the same.

How do I remove a class from a body tag?

To remove a class from the body element, call the classList. remove() method on the body object, e.g. document.


2 Answers

box.removeAttribute("class") should work.

like image 181
BJ Safdie Avatar answered Oct 11 '22 02:10

BJ Safdie


The nicest way to set classes with Javascript is to use the className property:

// to add
box.className = 'move';
// to remove
box.className = '';
like image 24
lonesomeday Avatar answered Oct 11 '22 02:10

lonesomeday