Adding and removing classes with javascript

1

I wanted to know how to add and remove a class in html elements with javascript

<div>
 <ul>
  <li class="Active">1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
 </ul>
</div>
    
asked by anonymous 10.08.2016 / 03:47

1 answer

1

Seven the element's class to empty.

// Pega o primeiro li e remove a classe
document.getElementsByName("li")[0].className = "";

If you want to keep some class, reset the class:

document.getElementsByName("li")[0].className = "";
document.getElementsByName("li")[0].className = "classe_que_fica";

I hope I have helped!

Any questions, just leave a comment.

    
10.08.2016 / 04:00