Posted By


iroybot on 07/11/10

Tagged


Statistics


Viewed 545 times
Favorited by 1 user(s)

getElementsByClass


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.