Skip to content

Instantly share code, notes, and snippets.

@hcmn
Created July 13, 2012 21:11
Show Gist options
  • Save hcmn/3107474 to your computer and use it in GitHub Desktop.
Save hcmn/3107474 to your computer and use it in GitHub Desktop.
Javascript: separating multiple class names
// ----------------------------------------------------------------------------
// 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
// ----------------------------------------------------------------------------
@hcmn
Copy link
Author

hcmn commented Jul 13, 2012

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment