Posted By

iroybot on 07/11/10


Tagged

javascript js getElementsByClass


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

iroybot
nerdfiles


getElementsByClass


 / Published in: JavaScript
 

  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.