Elementals.js

Latest Version: 3.0 RC 1 17 July 2017

Method _.Class.swap

Summary:

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

Calling Convention:
_.Class.swap(Element, [String || Array], [String || Array])
Parameters:
Element
The element to remove classes from
String
A string containing a single class
Array
An array of class strings
Returns:
_.Class.remove || _.Class.add

Example #1 -- String

HTML:

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

JavaScript:

var p = document.getElementById('test');
_.Class.swap(p, 'one', 'two');
// p#test no longer has the class 'one' on it, but now has class 'two'.

Example #2 -- Array

HTML:

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

JavaScript:

var p = document.getElementById('test');
alert(_.Class.swap(p, ['one', 'three', 'four'], ['five', 'six']);
// p#test is now class="two five six"
// alert would report true since actions were performed.

Advertisement