DEV Community

Falaye Promise
Falaye Promise

Posted on

The Document Object Model (DOM)

Hey there! Let's jump into the world of the Document Object Model, or DOM for short. If you're already familiar with HTML, CSS, and JavaScript, that's great! But even if you're new to these, we'll cover the basics to get you started.

Imagine a website as a house. HTML is the blueprint, defining the structure of the rooms, doors, and windows. CSS is the interior decorator, making everything look snazzy with colors, fonts, and layouts. Finally, JavaScript is like the electrician and plumber, adding interactivity – like turning on lights (changing content) when you click a button.HTML, CSS and JS are the building blocks of the web.

Javascript and the DOM

So, you may ask, how does JavaScript make a webpage interactive? This is where the DOM comes in. JavaScript needs a way to understand the content of the webpage, which has been defined by HTML. The DOM acts as a bridge, creating a tree-like structure of all the elements on the page and making it accessible to JavaScript.

The DOM structure is hierarchical, like an upside-down family tree. The entire document is the root node, with branches for different elements. The cool thing is, most browsers have built-in DOM viewers (like the Elements tab in Chrome DevTools) to help you as the developer to visualize this structure.

The DOM can be navigated with different programming languages but of all javascript is the most common.

What can you do with the DOM?

  1. Change content: Update text, add new elements, or remove existing ones.
  2. Style manipulation: Control the look and feel of the page by changing fonts, colors, and backgrounds.
  3. Respond to user interaction: Make elements react to clicks, hovers, or other user actions.

Structure of the DOM

We mentioned the DOM tree – a visual representation of the webpage's elements. Each element in the DOM is like an object with properties and methods that let you access and manipulate its content.

Nodes: The Building Blocks of the DOM Tree
Every element on the webpage is a node in the DOM tree. The document itself is also the root node. Nodes aren't limited to elements, though. They can also be text content or attributes within elements.

Nodes come with built-in properties you can use to navigate and access other elements in the DOM. Here are a few:

  • node.childNodes - returns a nodelist containing all the child nodes of the selected node
  • node.firstChild - returns the first child of the selected node
  • node.lastChild - returns the last child of the selected node
  • node.parentNode - returns the parent node of the specified node
  • node.nextSibling - returns the node that comes directly after the specified node in their parent nodelist
  • node.previousSibling - returns the node that comes directly before the specified node in their parent nodelist
  • node.hasChildNodes - a method. Returns true or false indicating whether the selected node has any child nodes

The Document Object: The Entry Point
Remember the document being the root node? This "document object" also acts as the entry point for accessing other nodes in the DOM tree. It has its own methods and properties for manipulating the webpage content:

  • querySelectorAll - Grabs all elements matching a CSS selector. The selector could be class, ID, tag name, or even a combination of these.It returns a NodeList containing all the elements in the document that match that selector. Very powerful! because it can query class, id or tag.
  • createElement - creates a new HTML element of the specified type (e.g., "div", "p", "h1").
  • getElementById - Gets an element by its unique ID. It's a quick way to target a single element with a known ID. Very fast!
  • getElementsByTagName - Gets all elements with a specific tag name. It returns a NodeList containing all the elements in the document with that specific tag name.
  • appendChild - Adds a new child element to the end of another element.
  • innerHTML- Gets all the content inside an element.
  • addEventListener - Makes an element listen for events and run code when they happen. The listener function is triggered when a specific event like a click, mouseover, or keypress occurs on that element.
  • replaceChild- Takes two nodes as input. Replaces an existing child node of a parent node with a new child node.
  • insertBefore - Takes two nodes and a reference node as input. Inserts a new child node before a specified reference node within the same parent
  • setAttribute - Changes or sets the value of an element's attribute. This method sets the value of a specified attribute on an element.

Now you've got a foundational understanding of the DOM! We explored its role in bridging JavaScript and HTML, how it structures the webpage content, and how you can use it to manipulate the webpage dynamically. With this knowledge, you can start building more interactive and engaging web experiences!

Top comments (0)