Mr. Zeeshan
Mr. Zeeshan

Reputation: 1

How to toggle class by a button click?

How to toggle class by a button click?

How to remove a class on the next click from the same button?

  <button onclick="goldborder()" id="btn">Click here</button>

JS:

function goldborder() {
  let boxs = document.getElementById('box');
  boxs.classList.add('bordercolor');
}

Upvotes: 0

Views: 54

Answers (1)

Babar Khan
Babar Khan

Reputation: 350

You can try this. If the class exists it removes and if not it adds in it.

boxs.classList.toggle('bordercolor');

Upvotes: 2

Related Questions