Programming

Checking whether a class is defined in CSS

Most of modern JS frameworks allow to check, whether an element has a particular class. However, I could not find something in jQuery related to checking, whether ther's a particular class in CSS. So, after googling for a while, I found the function which does this extremely well. I'm republishing it to save your time:

function getDefinedCss(s){
 if(!document.styleSheets) return '';
 if(typeof s== 'string') s= RegExp('\\b'+s+'\\b','i'); // IE capitalizes html selectors 

 var A, S, DS= document.styleSheets, n= DS.length, SA= [];
 while(n){
 S= DS[--n];
 A= (S.rules)? S.rules: S.cssRules;
 for(var i= 0, L= A.length; i<L; i++){
 tem= A[i].selectorText? [A[i].selectorText, A[i].style.cssText]: [A[i]+''];
 if(s.test(tem[0])) SA[SA.length]= tem;
 }
 }
 return SA.join('\n\n');
}
This function accepts an name of class or id(sure, without . or # ). In case it founds something, it returns the citation from CSS. Try it yourself!
 
Latest Tweets
Follow us on Twitter