Posted By

avioli on 02/15/08


Tagged

javascript


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

SpinZ
geejayoh
o0110o


GetElementsByClassName (by brito)


 / Published in: Other
 

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

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: adix on February 15, 2008

Interesting how you and Dustin Diaz made the exact same function, isn't it? http://www.dustindiaz.com/getelementsbyclass/

Posted By: satts on February 17, 2008

is this more powerful than the $() function offered by libraries

You need to login to post a comment.