Add,Remove and Toggle class with Element.classList JavaScript

Last Updated On Sunday 10th Apr 2022
	const div = document.createElement('div');
div.outerHTML
	

The Above Creates the

	<div></div>
	

Add Class JavaScript

  • You can add multiple classes
	div.classList.add("a")
div.classList.add("b")
div.classList.add("c")

div.outerHTML
div.className
	
	<div class="a b c"></div>
"a b c"
	

Remove class JavsScript

	div.outerHTML
div.classList.remove("a")
div.outerHTML
	
	<div class="a b c"></div>
<div class="b c"></div>
	

Toggle class JavaScript

	div.className
div.classList.toggle("c")
div.className
	
	"b c"
"b"
	

Here’s the reference Link