-
-
Save joshrhoades/5464609 to your computer and use it in GitHub Desktop.
Functionality for checking and manipulating classes on DOM Elements (i.e., checking if an element has a class, removing a class from an element, and adding a class to an element. Updated to use better naming conventions, use array joins instead if string concats (operation speed), and boolean returns to more easily be able to tell when functions…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file Functionality for checking and manipulating classes on DOM Elements (i.e., checking if an element has a class, | |
* removing a class from an element, and adding a class to an element. Updated to use better naming conventions, | |
* use array joins instead if string concats (operation speed), and boolean returns to more easily be able to tell when | |
* functions performed successfully within an app | |
* @author Josh Rhoades <http://joshuarhoades.com/> | |
* @added 04/25/13 | |
* @version 1.0.0 | |
* @see {@link https://gist.github.com/jelmerdemaat/4107273|Original before fork} | |
* @see {@link http://www.avoid.org/?p=78} | |
*/ | |
/** | |
* Function to determine if an element has a class | |
* @method | |
* @author Josh Rhoades <http://joshuarhoades.com/> | |
* @param {DOMElement} elCheck DOM Element to check for `strClass` | |
* @param {string} strClass The class to check `elCheck` for | |
* @returns {boolean} Returns `true` if element has class, otherwise returns `false` | |
*/ | |
function checkClass(elCheck, strClass) { | |
return new RegExp(['(\\s|^)',strClass,'(\\s|$)'].join('')).test(elCheck.className); | |
} | |
/** | |
* Function to add a class to an element, only if that class is not already present on the element | |
* @method | |
* @author Josh Rhoades <http://joshuarhoades.com/> | |
* @param {DOMElement} elCheck DOM Element to target for adding a specific class | |
* @param {string} strClass The class to add to `elCheck` | |
* @returns {boolean} Returns `true` if class was added to element, otherwise returns `false | |
*/ | |
function addClass(elCheck, strClass) { | |
if (!checkClass(elCheck, strClass)) { | |
elCheck.className += (elCheck.className ? ' ' : '') + strClass; | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Function to remove a class from an element, only if the class is present on the element | |
* @method | |
* @author Josh Rhoades <http://joshuarhoades.com/> | |
* @param {DOMElement} elCheck DOM Element to target for removing a specific class | |
* @param {string} strClass The class to remove from `elCheck` if it is present | |
* @returns {boolean] Returns `true` if class was removed from element, otherwise returns `false` | |
*/ | |
function removeClass(elCheck, strClass) { | |
if (checkClass(elCheck, strClass)) { | |
elCheck.className = elCheck.className.replace(new RegExp(['(\\s|^)', strClass, '(\\s|$)'].join('')),' ').replace(/^\s+|\s+$/g, ''); | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI, normally I would wrap these in a namespace, but have removed that for the sake of portability and dropping into existing code if other people want to pick them up.