Created
July 13, 2012 21:11
-
-
Save hcmn/3107474 to your computer and use it in GitHub Desktop.
Javascript: separating multiple class names
This file contains 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
// ---------------------------------------------------------------------------- | |
// HasClassName | |
// credit: dzone.com | |
// Description : returns boolean indicating whether the object has the class name | |
// built with the understanding that there may be multiple classes | |
// | |
// Arguments: | |
// objElement - element to manipulate | |
// strClass - class name to add | |
// | |
function HasClassName(objElement, strClass) | |
{ | |
// if there is a class | |
if ( objElement.className ) | |
{ | |
// the classes are just a space separated list, so first get the list | |
var arrList = objElement.className.split(' '); | |
// get uppercase class for comparison purposes | |
var strClassUpper = strClass.toUpperCase(); | |
// find all instances and remove them | |
for ( var i = 0; i < arrList.length; i++ ) | |
{ | |
// if class found | |
if ( arrList[i].toUpperCase() == strClassUpper ) | |
{ | |
// we found it | |
return true; | |
} | |
} | |
} | |
// if we got here then the class name is not there | |
return false; | |
} | |
// | |
// HasClassName | |
// ---------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can also use pseudo class to select multiple class names if position in array is already known.
For example,
:first
:nth-child
:eq(2)
etc.