-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathgetElementsByClassName.js
34 lines (29 loc) · 1.06 KB
/
getElementsByClassName.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// If life was easy, we could just do things the easy way:
// var getElementsByClassName = function (className) {
// return document.getElementsByClassName(className);
// };
// But instead we're going to implement it from scratch:
var getElementsByClassName = function(className) {
var results = [];
var checkNodes = function(node) {
// If the element contains the class className
if (node.classList && node.classList.contains(className)) {
// Add it to our result
results.push(node);
}
// If the element contains child nodes
if (node.childNodes) {
// Loop through each child node
for (var i = 0; i < node.childNodes.length; i++){
checkNodes(node.childNodes[i])
}
// _.each(element.childNodes, function(item) {
// // Recursively call findElementsContainingClass until we've looped through all child nodes
// findElementsContainingClass(item);
// });
}
}
// Start our recursive function
checkNodes(document.body);
return results
}