What is a good approach to add class to a DOM element using JavaScript. And Remove also.
I came across this following codes for adding:
1:
Element.prototype.addClassName = function (cls) {
if (!this.hasClassName(cls)) {
this.className = [this.className, cls].join( );
}
};
2:
document.querySelector(element).classList.add(cls)
Both of them seem to work for me. What is the difference between them and which is the best?