Skip to main content

Command Palette

Search for a command to run...

[JavaScript] DOM - Find a DOM Node

Updated
•View as Markdown

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;
}

getElementByAttribute.PNG

JavaScript

Part 1 of 50
\n \n \n

The DOM is cool for man...","author":{"@type":"Person","name":"Jayden Park","url":"https://hashnode.com/@longmonkey","image":"https://cdn.hashnode.com/res/hashnode/image/upload/v1649824772882/oiYj1qZQ0.png"},"publisher":{"@type":"Organization","name":"Jayden's Blog","url":"https://jaydenjpark.hashnode.dev"},"datePublished":"2022-08-18T16:18:50.039Z","dateModified":"2022-08-18T17:56:45.483Z","keywords":"JavaScript, DOM, Beginner Developers"}