Posted By

brianyang on 06/09/10


Tagged


Versions (?)

getElementsByClass


 / Published in: JavaScript
 

from dustin diaz

  1. function getElementsByClass(searchClass,node,tag) {
  2. var classElements = new Array();
  3. if ( node == null )
  4. node = document;
  5. if ( tag == null )
  6. tag = '*';
  7. var els = node.getElementsByTagName(tag);
  8. var elsLen = els.length;
  9. var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  10. for (i = 0, j = 0; i < elsLen; i++) {
  11. if ( pattern.test(els[i].className) ) {
  12. classElements[j] = els[i];
  13. j++;
  14. }
  15. }
  16. return classElements;
  17. }

Report this snippet  

You need to login to post a comment.