Toggle css classes in vanilla js

// Browser support - Chrome 8.0 - Firefox 3.6 - IE 10 - Opera 11.50 - Safari 5.1 // Use https://github.com/eligrey/classList.js/blob/master/classList.js for IE support ti version 8 // Example HTML // <div class='your-class-name opened'></div> // querySelector always returns the first element matched $element = document.querySelector('your-class-name') // add a class $element.classList.add('active') // remove a class $element.classList.remove('opened') // toggle a class $element.classList.toggle('selected') // toggle a class $element.classList.toggle('selected')
Use cases for various class manipulations using the vanilla js selector
.toggle()
.add()
.remove()
.contains()

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.