1document.getElementById('myButton').onclick = function() {
2 this.classList.toggle('active');
3}
1$('.click-class').click(function() {
2 $('.class-toggle').toggle();
3 $('.class-toggle').toggleClass('add-class');
4})
1var menu = document.querySelector('.menu') // Using a class instead, see note below.
2menu.classList.toggle('hidden-phone');
1// html <div class="parentElm" id="addActive"></div>
2
3// js
4function toggleClass(){
5 // get the properties of an element
6 var element = document.getElementById("addActive");
7
8// check if the element have class list
9 if (element.classList) {
10 // add active class if true
11 element.classList.toggle("active");
12 } else {
13 var classes = element.className.split(" ");
14 var i = classes.indexOf("active");
15 }
16 }
1document.getElementById('myButton').onclick = function() {
2
3 var className = ' ' + myButton.className + ' ';
4
5 if ( ~className.indexOf(' active ') ) {
6 this.className = className.replace(' active ', ' ');
7 } else {
8 this.className += ' active';
9 }
10}