GetElementsByClassName (by brito)


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



Copy this code and paste it in your HTML
  1. document.getElementsByClassName = function(searchClass, node, tag){
  2. var classElements = [];
  3. node = node || document;
  4. var pattern = new RegExp('(^|\\s)' + searchClass+ '(\\s|$)');
  5. var els = (!tag && node.all) || node.getElementsByTagName(tag || '*');
  6. var elsLen = els.length;
  7. for (var i=0, j=0;i<els.length;i++) {
  8. if (pattern.test(els[i].className)) {
  9. classElements[j] = els[i];
  10. j++;
  11. }
  12. }
  13. return classElements;
  14. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.