How to remove class name on javascript I tried to remove child element class name but I'm not able to do right code.Can any one help me.
I tried bellow code structure-
html
<div id="items">
<div class="item active">item-1</div>
<div class="item ">tem-2</div>
<div class="item ">tem-3</div>
</div>
script
document.getElementById('items').classList.remove("active");
classList just gets the classlist for the div with the id. Go over the child nodes.
document.getElementById('items').childNodes.forEach(e => e.classList.remove('active'))
Some older browser might not allow forEach one the nodeList
var children = document.getElementById('items').childNodes;
for (var i = 0; i < children.length; i++) {
children[i].classList.remove('active')
}
Uncaught TypeError: e.classList is undefined
Ive added a version that should work on all browsers :)
what about
document.querySelector('#items').classList.remove('active');
@ajithlal classList is again pointing to the parent div, and not the children :)
You can test it in the console right on this page
var children = document.getElementById('root').childNodes;
for (var i = 0; i < children.length; i++) {
if (children[i].classList) {
children[i].classList.remove('xl:tw-flex-1')
}
}
unfortunately, same error occurred
How is the error the same when there is no variable called e? Perhaps check if there is a classList like in my last example.
Uncaught TypeError: children[i].classList is undefined
getElementById("MyID"). className. replace(/\bMyClass\b/,''); where MyID is the ID of the element( https://www.njmc.website/ ) and MyClass is the name of the class you wish to remove.
Hmm. What browser are you using? I cannot reproduce that error.
Does it work in this fiddle? The first element should not be red
https://jsfiddle.net/1sayLk9n/
Please sign in or create an account to participate in this conversation.