Posted By

mifly on 02/29/08


Tagged

javascript


Versions (?)

Who likes this?

16 people have marked this snippet as a favorite

SpinZ
Wiederkehr
localhorst
adix
mifly
f0vela
joomla
hans
Jman
nerdfiles
wizard04
bobbym245
seanpowell
guo3823538
wirenaught
o0110o


The Ultimate getElementsByClassName - Robert’s talk - Web development and Internet trends


 / Published in: JavaScript
 

URL: http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/

  1. function getElementsByClassName(oElm, strTagName, strClassName){
  2. var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
  3. var arrReturnElements = new Array();
  4. strClassName = strClassName.replace(/\-/g, "\\-");
  5. var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
  6. var oElement;
  7. for(var i=0; i<arrElements.length; i++){
  8. oElement = arrElements[i];
  9. if(oRegExp.test(oElement.className)){
  10. arrReturnElements.push(oElement);
  11. }
  12. }
  13. return (arrReturnElements)
  14. }

Report this snippet  

You need to login to post a comment.