/ Published in: JavaScript

Similar to [Get Elements by Class Name]( http://snipplr.com/view/1696/get-elements-by-class-name/), but with optional "tag" parameter. Performance is increased by first retrieving the desired tag if available.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
getElementsByClassName: function(classname, baseNode, tag) { baseNode = baseNode || document.getElementsByTagName("body")[0]; var a = []; var re = new RegExp('\\b' + classname + '\\b'); var els = baseNode.getElementsByTagName(tag || "*"); for (var i = 0, j = els.length; i < j; i++) if (re.test(els[i].className)) a.push(els[i]); return a; }
Comments
