Posted By

quan on 06/11/10


Tagged

javascript


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

okmores


HasClassName


 / Published in: JavaScript
 

check to see if there's a class of an element. or search a class if the object has more than one class

  1. // ----------------------------------------------------------------------------
  2. // HasClassName
  3. //
  4. // Description : returns boolean indicating whether the object has the class name
  5. // built with the understanding that there may be multiple classes
  6. //
  7. // Arguments:
  8. // objElement - element to manipulate
  9. // strClass - class name to add
  10. //
  11. function HasClassName(objElement, strClass)
  12. {
  13.  
  14. // if there is a class
  15. if ( objElement.className )
  16. {
  17.  
  18. // the classes are just a space separated list, so first get the list
  19. var arrList = objElement.className.split(' ');
  20.  
  21. // get uppercase class for comparison purposes
  22. var strClassUpper = strClass.toUpperCase();
  23.  
  24. // find all instances and remove them
  25. for ( var i = 0; i < arrList.length; i++ )
  26. {
  27.  
  28. // if class found
  29. if ( arrList[i].toUpperCase() == strClassUpper )
  30. {
  31.  
  32. // we found it
  33. return true;
  34.  
  35. }
  36.  
  37. }
  38.  
  39. }
  40.  
  41. // if we got here then the class name is not there
  42. return false;
  43.  
  44. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: messup000 on June 15, 2010

I would suggest removing the toUpperCase since css class names are case sensitive.

You need to login to post a comment.