-4

What's the best and easy way to add/remove a class to an element which has several classes? I want to do that in pure javascript. I've seen some topics but there was no single solution which everyone agrees to use de-facto. Is there any? Or should I just iterate over all the class, remember them and them add/remove one? Is there more simple and straightforward solution in pure javascript?

ako25
  • 889
  • 1
  • 8
  • 11
  • have you searched on the web? – gitaarik Jan 02 '17 at 10:22
  • While this *can* be answered in a purely theoretical manner it would be far more useful to you, and others in the future, if you gave a specific snippet of HTML, and the JavaScript you're using to retrieve any given element, for us to demonstrate upon, That way you have a concrete example and we don't have to manufacture one for you. – David Thomas Jan 02 '17 at 10:23
  • @Manish, open your eyes. – ako25 Jan 02 '17 at 10:24

1 Answers1

0

You can do this by,

elm = document.getElementById("Id");
elm.classList.add("class","name","all"); //to add
elm.classList.remove("class","name","all"); //to remove

I would request you to read JavaScript documentations/tutorials, its the first thing you will see in any tutorials.

putvande
  • 15,068
  • 3
  • 34
  • 50
Prajwal
  • 3,930
  • 5
  • 24
  • 50