Elementals.js

Latest Version: 2.0 RC 3 14 March 2017

Method _.classToggle

Summary:

Swaps a class or list of classes from an element for another class or list of classes.

Calling Convention:
_.classToggle(Element, [String || Array])
Parameters:
Element
The element to remove classes from
String
A string containing a single class
Array
An array of class strings
Returns:
True if class or classes were added
False if class or classes were removed

Example #1 -- String

HTML:

<p id="test" class="one"></p>

JavaScript:

var p = document.getElementById('test');
_.classToggle(p, 'two');
// p#test now has class 'two'
_.classToggle(p, 'one');
// p#test no longer has class 'one' since it started out with it.

While this does accept arrays you should be aware that if any array entry is removed, NONE will be set! Values will only be set if none are removed. Whether this should be considered the correct behavior or if steps should be taken to be a literal toggle is still under debate, so for now do not rely on the Array behaviors as it may change.