THE WORLD'S LARGEST WEB DEVELOPER SITE

W3.JS Add Class

Add classes to HTML elements

Add a class:

w3.addClass(selector,'class')

Add multiple classes:

w3.addClass(selector,'class1 class2 class3...')

Add Class by Id

Add the "marked" class to an element with id="London":

Example

<button onclick="w3.addClass('#London','marked')">Add Class</button>

Try It Yourself »


Add Class by Tag

Add the "marked" class to all <h2> elements:

Example

<button onclick="w3.addClass('h2','marked')">Add Class</button>

Try It Yourself »



Add Class by Class

Add the "marked" class to an elements with class="London":

Example

<button onclick="w3.addClass('.city','marked')">Add Class</button>

Try It Yourself »


Add Multiple Classes

To add multiple classes to an element, separate each class with a space.

Add both "class1" and "class2" to an element with id="London":

Example

<button onclick="w3.addClass('#London','class1 class2')">Add Classes</button>

Try It Yourself »