Created
October 6, 2010 06:13
-
-
Save kgn/612908 to your computer and use it in GitHub Desktop.
Get html elements by class name.
This file contains hidden or 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
| /* | |
| There are many scripts out there for this but | |
| many of them use a regular expression with \b. | |
| The problem with this is \b matches more then | |
| white space, like '-'. So if you want to find | |
| all elements with the class 'expando' elements | |
| with 'expando-button' will be returned. | |
| This function fixes this problem. | |
| */ | |
| function getElementsByClassName(node, classname){ | |
| var a = []; | |
| var re = new RegExp(' ' + classname + ' '); | |
| var els = node.getElementsByTagName('*'); | |
| for(var i=0,j=els.length; i<j; i++){ | |
| if(re.test(' '+els[i].className+' ')){ | |
| a.push(els[i]); | |
| } | |
| } | |
| return a; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment