Skip to content

Commit

Permalink
Added Element prototype method getElementsByClassName
Browse files Browse the repository at this point in the history
  • Loading branch information
etler committed May 19, 2014
1 parent 1fbd7b1 commit fd42583
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/DOM/Element.js
Expand Up @@ -118,6 +118,33 @@ Element = (function () {
});
};

Element.prototype.getElementsByClassName = function (string) {
var child, index, childClassName, classes, hasClasses, matches;
hasClasses = function (childClasses, matchClasses) {
var index;
for (index = 0; index < matchClasses.length; index++) {
if (childClasses.indexOf(matchClasses[index]) === -1) {
return false;
}
}
return true;
}
classes = string.split(' ');
matches = [];
for (index = 0; index < this.childNodes.length; index++) {
child = this.childNodes[index];
if (!(child instanceof Element)) {
continue
}
matches = matches.concat(child.getElementsByClassName(string));
childClassName = child.getAttribute('class');
if (childClassName && hasClasses(childClassName.split(' '), classes)) {
matches.push(child);
}
}
return matches;
}

Element.prototype.querySelector = function (string) {
return this.querySelectorAll(string)[0];
}
Expand Down

0 comments on commit fd42583

Please sign in to comment.