Posted By

jatkins on 11/09/11


Tagged

css elements DOM classes


Versions (?)

getElementsFromClassName


 / Published in: JavaScript
 

Released into the public domain.

  1. function find_index(array, string) {
  2. var i = 0;
  3. for(i=0;i<array.length;i++) {
  4. if(array[i]==string) break;
  5. }
  6. return array[i] == string ? i : -1;
  7. }
  8.  
  9. function getElementsFromClassName(classToMatch, pNode) {
  10. cNodes = (typeof pNode=='undefined'?document.body:pNode).getElementsByTagName('*');
  11. cMatches = [];
  12. for(var c=0;c<cNodes.length;c++) {
  13. cNodeClasses = cNodes[c].className;
  14. if(cNodeClasses&&find_index(cNodeClasses.toString().split(' '), classToMatch)!=-1)
  15. cMatches.push(cNodes[c]);
  16. }
  17. return cMatches;
  18. }

Report this snippet  

You need to login to post a comment.