Posted By


otaqui on 02/08/08

Statistics


Viewed 594 times
Favorited by 0 user(s)

GetElementsByClassName


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



Copy this code and paste it in your HTML
  1. function getElementsByClassName(className, tag, elm){
  2. var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
  3. var tag = tag || "*";
  4. var elm = elm || document;
  5. var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
  6. var returnElements = [];
  7. var current;
  8. var length = elements.length;
  9. for(var i=0; i<length; i++){
  10. current = elements[i];
  11. if(testClass.test(current.className)){
  12. returnElements.push(current);
  13. }
  14. }
  15. return returnElements;
  16. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.