Question #470   Submitted by Answiki on 05/19/2021 at 08:02:42 PM UTC

How to add a class to a given element in JavaScript?

Answer   Submitted by Answiki on 05/19/2021 at 08:02:53 PM UTC

In JavaScript, the simplest way to add a class is to use the method .add() on the property .classList:

var element = document.getElementById('ID');
element.classList.add('myClass');

6 events in history
Answer by Answiki on 05/19/2021 at 08:02:53 PM

In JavaScript, the simplest way to add a class is to use the method .add() on the property .classList:

var element = document.getElementById('ID');
element.classList.add('myClass');

Question by Answiki 05/19/2021 at 08:02:42 PM
How to add a class to a given element in JavaScript?
Question by Answiki 05/19/2021 at 08:02:26 PM
How to add class using pure JavaScript?
Question by Answiki 05/19/2021 at 08:02:21 PM
How to add class using JavaScript?
Question by Answiki 05/19/2021 at 08:02:11 PM
How to add class using vanilla js?
Answer by Answiki on 05/19/2021 at 08:02:02 PM

In JavaScript, the simplest way to add a class is to use the method .add() on the property .classList:

var element = document.getElementById('ID');
element.classList.add('myClass');

# ID Query URL Count

Icons proudly provided by Friconix.