Created
November 6, 2012 19:00
-
-
Save baamenabar/4026742 to your computer and use it in GitHub Desktop.
Dustin Diaz's get element by class JS function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
taken from http://www.dustindiaz.com/top-ten-javascript | |
*/ | |
function getElementsByClass(searchClass,node,tag) { | |
var classElements = new Array(); | |
if ( node == null ) | |
node = document; | |
if ( tag == null ) | |
tag = '*'; | |
var els = node.getElementsByTagName(tag); | |
var elsLen = els.length; | |
var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)'); | |
for (i = 0, j = 0; i < elsLen; i++) { | |
if ( pattern.test(els[i].className) ) { | |
classElements[j] = els[i]; | |
j++; | |
} | |
} | |
return classElements; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment