Posted By

perelin on 07/19/06


Tagged


Versions (?)

Who likes this?

9 people have marked this snippet as a favorite

xaviaracil
mate
jkochis
Leech
dmarten
vali29
hans
seanpowell
guo3823538


getElementsByClassName


 / Published in: JavaScript
 

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

  1. /*
  2.   Written by Jonathan Snook, http://www.snook.ca/jonathan
  3.   Add-ons by Robert Nyman, http://www.robertnyman.com
  4. */
  5.  
  6. function getElementsByClassName(oElm, strTagName, strClassName){
  7. var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
  8. var arrReturnElements = new Array();
  9. strClassName = strClassName.replace(/\-/g, "\\-");
  10. var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
  11. var oElement;
  12. for(var i=0; i<arrElements.length; i++){
  13. oElement = arrElements[i];
  14. if(oRegExp.test(oElement.className)){
  15. arrReturnElements.push(oElement);
  16. }
  17. }
  18. return (arrReturnElements)
  19. }

Report this snippet  

You need to login to post a comment.