• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Forum
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / CSS / Toggle a class for an HTML element

Toggle a class for an HTML element

Toggle a class for an HTML element

toggleClass toggles a class for an HTML element.

Use Element.classList and DOMTokenList.toggle() to toggle the specified class for the element.

const toggleClass = (el, className) => el.classList.toggle(className);

// EXAMPLE
toggleClass(document.querySelector('p.special'), 'special');
// The paragraph will not have the 'special' class anymore

Source

https://www.30secondsofcode.org/js/s/toggle-class

CSS

Related Snippets:

  • Get the computed style of an element
  • Find all matching elements on a page with document.querySelectorAll()
  • Get the default value of a CSS property with JavaScript
  • Get the Children of an Element

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2022 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us