logo
logo

Toggle Class

Beginner

Toggle Class

Create an exported function
toggleClass
that takes in two arguments:
  1. An HTML element

  2. A string representing the class to toggle

The function should toggle the class on the element, adding it if it doesn't exist and removing it if it does.

Example usage:

// Given an HTML element <div class="my-class"></div>
const element = document.querySelector('.my-class');
toggleClass(element, 'active'); // <div class="my-class active"></div>
toggleClass(element, 'active'); // <div class="my-class"></div>

Console
Submit
Solution
00:00