Elementals.js

Latest Version: 3.7 Final 20 December 2018

Method _.Class.toggle

Summary:

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

Calling Convention:
_.Class.toggle(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');
_.Class.toggle(p, 'two');
// p#test now has class 'two'
_.Class.toggle(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.

Advertisement