Posted By

rhlowe on 06/21/07


Tagged

missingfunctions


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

duandan
rodi01
shapeshifta


getElementsByClassName


 / Published in: JavaScript
 

  1. // Implementation of the missing (nonexistant) 'document.getElementsByClassName()
  2. document.getElementsByClassName = function(cl) {
  3. var retnode = [];
  4. var myclass = new RegExp('\\b'+cl+'\\b');
  5. var elem = this.getElementsByTagName('*');
  6. for (var i = 0; i < elem.length; i++) {
  7. var classes = elem[i].className;
  8. if (myclass.test(classes)) retnode.push(elem[i]);
  9. }
  10. return retnode;
  11. };
  12.  
  13. /**
  14.  * call just like document.getElementById('') or document.getElementsByTagName('')
  15.  */

Report this snippet  

You need to login to post a comment.