I have some DIVs on a page. Each has 3 classes: grid_#, push_# and pull_# where #
is an actual number.
I need to replace each class separately, i.e. r
If element has multiple classes, you need to use split
:
var old = document.getElementById('elementId');
var old_class = old.className.split(' ')[0]; // first class
What split does is that it creates an array by splitting string (class in this case) which you can later access by its index eg [0]
for first class.
If element has just one class, you simply use className
:
var cls = old.className;