[JavaScript] DOM - Find a DOM Node
How would you implement getElementsByAttribute?
- HTML
<!DOCTYPE html>
<html>
<head>
<title>The DOM is cool</title>
<script type="text/javascript" src="activity.js"></script>
</head>
<body>
<h1 class="header">The DOM is cool for many reasons.</h1>
<ul>
<li category="feature">You can see how pages are made</li>
<li category="feature">You can manipulate it</li>
<li>You can listen for user interactions</li>
</ul>
</body>
</html>
- JavaScript
function getElementByAttribute(att, value) {
const all = document.getElementsByTagName("*");
let found = [];
for (let i = 0; i < all.length; i++) {
if (all[i].getAttribute(att) === value) {
found.push(all[i]);
}
}
return found;
}