Working with CSS classes in JavaScript

Array of classes

Given an element:

<p id="elem" class="www ggg zzz"></p>

Find out the number of its classes.

Given an element:

<p id="elem" class="www ggg zzz"></p>

Loop through its classes.

Adding classes

Given an element:

<p id="elem" class="www ggg zzz"></p>

Add class xxx to it.

Removing classes

Given an element:

<p id="elem" class="www ggg zzz"></p>

Remove the class www and the class zzz from it.

Checking classes

Given an element:

<p id="elem" class="www ggg zzz"></p>

Check if it has the class ggg.

Toggling classes

Given an element. Add the class www to it if it doesn't exist and remove it if it does.

enru