Sobes.tech
Junior — Middle

Explain what the DOM model is in the context of JavaScript

sobes.tech AI

Answer from AI

The DOM (Document Object Model) is a programming interface for HTML and XML documents that represents the structure of a document as a tree of objects. In the context of JavaScript, the DOM allows scripts to dynamically access and modify the content, structure, and styles of a web page.

Each element, attribute, and text in HTML is represented as an object in the DOM tree. JavaScript can use DOM methods and properties to:

  • Search for elements (e.g., getElementById, querySelector)
  • Change the content and attributes of elements
  • Add or remove nodes
  • Handle events

Example of changing an element's text:

const heading = document.getElementById('title');
heading.textContent = 'New Title';

The DOM provides a link between the document's structure and scripts, enabling the creation of interactive web pages.