× Search About Posts Code Music Links
Blank Try
experiment
lots
learn
more

JS for CSS

Updated on
Last site update: 8 May 2024

Unfinished article

Here a quick way to toggle a class on and off of an element.

1<div class="content" onclick="this.classList.toggle('x')">
2</div>

This will add the class of x to the element when it’s clicked and remove it when it’s clicked again.

1<div class="content x">
2</div>

You can then add some CSS to the class of x:

1.x {
2    color: red;
3}

This toggle method can be used in a function. The below toggles the class of change on or off.

1function hamburger(x) {
2    x.classList.toggle("change");
3}

See this example in action.

Dev.to article My codepen on changing theme colours with JS