We support #BlackLivesMatters !
If you support us then follow us by clicking here and following us!

How to add/remove class names from elements in javascript or es6 using className?

Get support on JavaScript,jQuery, AJAX and other related technology.
Post Reply
admin
Site Admin
Posts: 44

How to add/remove class names from elements in javascript or es6 using className?

Post by admin » Thu Aug 22, 2019 2:45 pm

To a single element:

Code: Select all

var el = document.getElementById('#foo');
el[i].className = 'my-dynamic-class';
But that’ll override the existing class, the following appends it to the old classes:

Code: Select all

var el = document.getElementById('#foo');
el[i].className += ' my-dynamic-class';
Or:

Code: Select all

el.className = d.className + " my-class";

Post Reply