1. javascript
  2. /basics
  3. /dom

Introduction to JavaScript's Document Object Model (DOM)

Introduction

The Document Object Model, or DOM for short, is a programming interface for HTML and XML documents. It allows developers to access and manipulate the content and structure of a website using JavaScript.

What is the DOM?

The DOM represents a document (such as an HTML or XML document) as a tree-like structure. Each element in the document is represented as a node in the tree. For example, an HTML document with a single <p> element would be represented as a tree with a single text node as a child of the <p> element.

The DOM provides a way for JavaScript to access and manipulate the content and structure of a document. This means that you can use JavaScript to change the text of an element, add or remove elements from the tree, and more.

Accessing Elements in the DOM

The easiest and most beginner-friendly way to access elements in the DOM is by using the document.getElementById() method. This method takes a single argument, the id of the element you want to access, and returns the element as an object. For example:

<p id="my-paragraph">This is some text.</p>

<script>
  var myParagraph = document.getElementById("my-paragraph");
  console.log(myParagraph);
</script>

This code would log the <p> element to the console as an object. Once you have an element, you can use various properties and methods to access and manipulate it.

You can also access elements by their tag name, class name, or CSS selectors using document.getElementsByTagName(), document.getElementsByClassName(), document.querySelector(), document.querySelectorAll() respectively.

Additionally, libraries such as jQuery provide an alternative way to manipulate the DOM. These libraries can make it easier to work with the DOM and provide a more consistent API across different browsers. Some examples include:

  • Selecting elements by CSS selectors

  • Adding and removing classes

  • Making AJAX requests

  • Creating and manipulating elements

  • Event handling

  • Traversing the DOM

  • Chaining multiple methods together

Changing the text of an element in jQuery would look like this:

<p id="my-paragraph">This is some text.</p>

<script>
  $("#my-paragraph").text("This is some new text.");
</script>

Manipulating Elements in the DOM

Once you have access to an element in the DOM, you can use its properties and methods to manipulate it. Here are a few common examples:

  • Changing the text of an element: Use the innerHTML property to change the text of an element. For example:
<p id="my-paragraph">This is some text.</p>

<script>
  var myParagraph = document.getElementById("my-paragraph");
  myParagraph.innerHTML = "This is some new text.";
</script>
  • Changing the style of an element: Use the style property to change the style of an element. For example:
<p id="my-paragraph">This is some text.</p>

<script>
  var myParagraph = document.getElementById("my-paragraph");
  myParagraph.style.color = "red";
</script>
  • Adding an element to the DOM: Use the createElement() and appendChild() methods to add a new element to the DOM. For example:
<div id="my-div">
  <p id="my-paragraph">This is some text.</p>
</div>

<script>
  var myDiv = document.getElementById("my-div");
  var newParagraph = document.createElement("p");
  newParagraph.innerHTML = "This is a new paragraph.";
  myDiv.appendChild(newParagraph);
</script>
  • Removing an element from the DOM: Use the removeChild() method to remove an element from the DOM. For example:
<div id="my-div">
  <p id="my-paragraph">This is some text.</p>
</div>

<script>
  var myDiv = document.getElementById("my-div");
  var myParagraph = document.getElementById("my-paragraph");
  myDiv.removeChild(myParagraph);
</script>

Note that for the above manipulation examples to work, the DOM has to be previously rendered, which means the script needs to be executed after the elements have been rendered in the browser.

Another way to ensure that the DOM is ready before executing scripts is to use the defer attribute in the script tag. The defer attribute tells the browser to execute the script after the page has finished parsing and loading. For example:

<script defer>
  var myDiv = document.getElementById("my-div");
  // DOM manipulation code here
</script>

Events

An important aspect of the DOM is the ability to respond to events. Events are actions that occur on a web page, such as a user clicking a button or a page finishing loading. The DOM provides a way to attach event listeners to elements, which allows you to run a function when a specific event occurs.

For instance, you can attach a click event listener to a button element, which will run a function when the button is clicked.

<button id="my-button">Click Me</button>
<script>
    var myButton = document.getElementById("my-button");
    myButton.addEventListener("click", function() {
      alert("Button was clicked!");
    });
</script>

There are many events you can listen to such as mouseover, mouseout, change, submit, load, and more. The DOM provides a way to respond to these events in a dynamic way, allowing you to create interactive web pages.

Conclusion

  • The DOM represents a document as a tree-like structure, with each element in the document represented as a node in the tree.

  • The DOM provides a way for JavaScript to access and manipulate the content and structure of a document.

  • document.getElementById() is the easiest and most beginner-friendly way to access elements in the DOM, but you can also access elements by their tag name, class name, or CSS selectors.

  • Once you have access to an element in the DOM, you can use its properties and methods to manipulate it.

  • The DOM also provides a way to respond to events, such as user clicks or page loading, by attaching event listeners to elements.

  • Libraries like jQuery can make it easier to work with the DOM and provide a more consistent API across different browsers.

  • To ensure that the DOM is ready before executing scripts, you can use the defer attribute in the script tag.

  • Understanding the DOM is essential for building dynamic, interactive websites with JavaScript.