Forum Moderators: open
document.getElementById('whatever').classList.remove(class1, class2);
document.getElementById('whatever').classList.add(class3, class4);
// get the classList as an array of strings
var classList = document.getElementById("whatever").className.split(" "),
// removeList is the list of classes to remove
removeList = ['class1', 'class5'],
// addList is the list of classes to add
addList = ['class3', 'class3'];
// filter the classList, removing the items indicated in the removeList
classList = classList.filter(function (className) {
return removeList.indexOf(className) < 0;
});
// add the classes indicated in the addList (if they are not already in the list)
addList.forEach(function (className) {
if (classList.indexOf(className) < 0) {
classList.push(className);
}
});
// assign the new classList back to the DOM element
document.getElementById("whatever").className = classList.join();
$('#whatever').removeClass('class1 class2').addClass('class3 class4');
But if this is the only thing you're going to use jQuery for, it's not really worth loading that library just for that.