There's this situation in which you don't know a classname's value beforehand. You do know that the classname is, for example, the 3rd class of an html element.
If you know the classname's index in the classlist, but you don't know its exact value, then how to remove it?
I'd guessed something like this would work:
var elements = document.querySelector('.element');
for (var i = 0; i < elements.length; i++) {
var classes = elements[i].classList;
for (var j = 0; j < classes.length; j++) {
if (classes[2] !== null) {
elements[i].classList.delete(elements[i].classList[2])
elements[i].classList.add('classname')
}
}
}
How can you delete a classname by index of the classlist? And how to replace or toggle the value of the classname with this same index?
The effort a Jquery answer would be appreciated, but most of my curiosity goes out to a vanilla Javascript solution.