THE WORLD'S LARGEST WEB DEVELOPER SITE

W3.JS Toggle Class

Toggle the Class of HTML elements

Toggle a class (on/off):

w3.toggleClass(selector,'class')

Toggle between two classes:

w3.toggleClass(selector,'property','class','class')

Toggle Class by Id

Toggle between the "marked" class of an element with id="London":

Example

<button onclick="w3.toggleClass('#London','marked')">Toggle</button>

Try It Yourself »


Toggle Class by Tag

Toggle between the "marked" class of all <h2> elements:

Example

<button onclick="w3.toggleClass('h2','marked')">Toggle</button>

Try It Yourself »


Toggle Class by Class

Toggle between the "marked" class of all elements with class="city":

Example

<button onclick="w3.toggleClass('.city','marked')">Toggle</button>

Try It Yourself »


Toggle Multiple Classes

Toggle between the class name "class1" and "class2" of id="London:

Example

<button onclick="w3.toggleClass('#London','class1','class2')">Toggle</button>

Try It Yourself »