Toggle classes

<div class="row none" id="DivID">
......
</div>

CSS

.row.none{display:none}
.row.flex{display:flex;}

On clicking a radio button I want to replace the class “none” with “flex”.
I have tried using classList.addClass() and classList.removeClass() to no avail.
Any help appreciated.

Probably because the classList object does not implement addClass() or removeClass(), but add() and remove().

1 Like

You don’t really need to add one class and remove the other. You could just add the none class and then remove it (or just toggle it) assuming that .row already has a style of flex.

e.g.

.row { display: flex;}
.none {display: none;}
  const toggle = document.querySelector("#toggle");
  const divRow = document.querySelector("#DivID");
  toggle.addEventListener("click", hideRow);
  function hideRow() {
    divRow.classList.toggle("none");
  }

Basic example:
https://codepen.io/paulobrien/pen/jEOzxaB/886b22bca5e3181ceb5a8132f72d48ef

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.