Daily Archives: May 4, 2018

Changing Classes with JavaScript To dynamically change class(es) of an html element using Javascript, you can use classList method: document.getElementById(“MyElement”).classList.add(‘MyClass’); document.getElementById(“MyElement”).classList.remove(‘MyClass’); if ( document.getElementById(“MyElement”).classList.contains(‘MyClass’) ) { … } document.getElementById(“MyElement”).classList.toggle(‘MyClass’); To change all classes for an element: document.getElementById(“MyElement”).className = “MyClass”; document.getElementById(“MyElement”).className = “MyClass1 MyClass2”; The above commands will overwrite the existing classes; […]

Change an html element’s class with JavaScript and jQuery