We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

tylerhall on 11/20/06


Tagged

javascript class name


Versions (?)


Who likes this?

3 people have marked this snippet as a favorite

janetmck
vali29
hr1v


Get Elements By Class Name


Published in: JavaScript 


Returns a list of child nodes with the supplied class name.

  1. function getElementsByClassName(classname, node) {
  2. if(!node) node = document.getElementsByTagName("body")[0];
  3. var a = [];
  4. var re = new RegExp('\\b' + classname + '\\b');
  5. var els = node.getElementsByTagName("*");
  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.