Posted By


perelin on 07/19/06

Tagged


Statistics


Viewed 609 times
Favorited by 5 user(s)

getElementsByClassName


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.