Posted By

jasonseney on 04/23/10


Tagged

javascript DOM


Versions (?)

Javascript DOM - getElementsByClassName


 / Published in: JavaScript
 

Similar to Get Elements by Class Name, but with optional "tag" parameter. Performance is increased by first retrieving the desired tag if available.

  1. getElementsByClassName: function(classname, baseNode, tag) {
  2. baseNode = baseNode || document.getElementsByTagName("body")[0];
  3. var a = [];
  4. var re = new RegExp('\\b' + classname + '\\b');
  5. var els = baseNode.getElementsByTagName(tag || "*");
  6. for (var i = 0, j = els.length; i < j; i++)
  7. if (re.test(els[i].className)) a.push(els[i]);
  8. return a;
  9. }

Report this snippet  

You need to login to post a comment.