DEV Community

Nwafor Onyebuchi
Nwafor Onyebuchi

Posted on

DOM Cheatsheet for Beginners

1. Selecting Elements

  • Select by ID:
  const element = document.getElementById('myId');
Enter fullscreen mode Exit fullscreen mode
  • Select by Class Name:
  const elements = document.getElementsByClassName('myClass'); // returns a live HTMLCollection
Enter fullscreen mode Exit fullscreen mode
  • Select by Tag Name:
  const elements = document.getElementsByTagName('div'); // returns a live HTMLCollection
Enter fullscreen mode Exit fullscreen mode
  • Select using Query Selector:
  const element = document.querySelector('.myClass'); // selects the first matching element
Enter fullscreen mode Exit fullscreen mode
  • Select all using Query Selector All:
  const elements = document.querySelectorAll('.myClass'); // returns a static NodeList
Enter fullscreen mode Exit fullscreen mode

2. Creating and Inserting Elements

  • Create a new element:
  const newElement = document.createElement('div');
Enter fullscreen mode Exit fullscreen mode
  • Add text to the new element:
  newElement.textContent = 'Hello, World!';
Enter fullscreen mode Exit fullscreen mode
  • Insert into the DOM:
  document.body.appendChild(newElement); // appends to the end of the body
Enter fullscreen mode Exit fullscreen mode
  • Insert before a specific element:
  const referenceElement = document.getElementById('myId');
  document.body.insertBefore(newElement, referenceElement);
Enter fullscreen mode Exit fullscreen mode

3. Modifying Elements

  • Change an element's text:
  element.textContent = 'New Text';
Enter fullscreen mode Exit fullscreen mode
  • Change an element's HTML:
  element.innerHTML = '<strong>New HTML Content</strong>';
Enter fullscreen mode Exit fullscreen mode
  • Change styles:
  element.style.color = 'red';
  element.style.backgroundColor = 'blue';
Enter fullscreen mode Exit fullscreen mode
  • Change attributes:
  element.setAttribute('href', 'https://example.com');
Enter fullscreen mode Exit fullscreen mode

4. Removing Elements

  • Remove an element:
  const elementToRemove = document.getElementById('myId');
  elementToRemove.parentNode.removeChild(elementToRemove);
Enter fullscreen mode Exit fullscreen mode

5. Event Handling

  • Add an event listener:
  element.addEventListener('click', function() {
      alert('Element clicked!');
  });
Enter fullscreen mode Exit fullscreen mode
  • Remove an event listener:
  const handleClick = () => {
      alert('Element clicked!');
  };
  element.addEventListener('click', handleClick);
  element.removeEventListener('click', handleClick);
Enter fullscreen mode Exit fullscreen mode

6. Traversing the DOM

  • Parent Node:
  const parent = element.parentNode;
Enter fullscreen mode Exit fullscreen mode
  • Child Nodes:
  const children = element.childNodes; // includes all child nodes (text, comments, etc.)
Enter fullscreen mode Exit fullscreen mode
  • First Child:
  const firstChild = element.firstChild;
Enter fullscreen mode Exit fullscreen mode
  • Last Child:
  const lastChild = element.lastChild;
Enter fullscreen mode Exit fullscreen mode
  • Next Sibling:
  const nextSibling = element.nextSibling;
Enter fullscreen mode Exit fullscreen mode
  • Previous Sibling:
  const previousSibling = element.previousSibling;
Enter fullscreen mode Exit fullscreen mode

Happy coding!

Top comments (0)