Posted By

noah on 07/20/07


Tagged

regex css DOM whitespace className utilities selector mattkruse hasclass icanhasclass


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

heinz1959
tcmacdonald


Kruse's hasClass


 / Published in: JavaScript
 

URL: http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/b68cac304ee6de78/e445c1df18698a3f?lnk=gst&q=hasclass&rnum=3

This Matt Kruse's hasClass function.

  1. /*
  2.  ** Matt Kruse's hasClass, with slight modification
  3.  ** Determine if an object or class string contains a given class.
  4.   */
  5. function hasClass (obj, className) {
  6. if (typeof obj == 'undefined' || obj==null || !RegExp) { return false; }
  7. var re = new RegExp("(^|\\s)" + className + "(\\s|$)");
  8. if (typeof(obj)=="string") {
  9. return re.test(obj);
  10. }
  11. else if (typeof(obj)=="object" && obj.className) {
  12. return re.test(obj.className);
  13. }
  14. return false;
  15. }

Report this snippet  

You need to login to post a comment.