16.1 Introduction to DOM (15 mins)

  1. What is the DOM (Document Object Model)?

    • Definition: The Document Object Model (DOM) is a programming interface that represents the structure of an HTML or XML document as a tree of objects. Each element in the HTML is represented as an object in the DOM, and JavaScript can interact with these objects to manipulate the page dynamically.

    • The HTML-to-DOM Connection:

      • HTML is static, but JavaScript can interact with HTML via the DOM to make web pages dynamic, such as changing text, styles, or structure after the page has loaded.

    • Tree Structure: The DOM represents the document as a hierarchical tree, where the document itself is the root, and the HTML elements (tags like <div>, <p>, <h1>, etc.) are the branches.

Example:

  • Given the following HTML structure:

  • The DOM represents this as:

    • html

      • body

        • h1 (with text content "Welcome to My Website")

        • p (with text content "This is a paragraph.")

  1. Why is the DOM Important?

    • Dynamic Web Pages: The DOM allows JavaScript to dynamically update the content, structure, and styling of a web page.

    • Interactivity: Through the DOM, JavaScript can react to user inputs like clicks, keypresses, and form submissions.

    • Manipulation: You can add, remove, or modify elements and their attributes or styles via the DOM, making web pages responsive to user actions.

  2. Basic DOM Manipulation in JavaScript:

    • JavaScript can be used to access and manipulate DOM elements using various methods and properties.

    • Accessing DOM Elements:

      • document.getElementById(): Access an element by its id.

      • document.querySelector(): Select the first element that matches a CSS selector.

      Example:

    • Modifying DOM Elements:

      • Changing Text: Use .innerText or .textContent to modify the text inside an element.

      • Example:

  3. DOM Tree Example:

    • Visualizing the DOM tree:

    • In the DOM, this would be represented as:

      • html

        • head

          • title ("Document")

        • body

          • div

            • p ("Hello, World!")


Student Activity (15 mins):

Objective: Students will explore the DOM by selecting and modifying HTML elements using JavaScript.


Step-by-Step Activity:

  1. Create an HTML file (e.g., dom_intro.html) and write JavaScript code to manipulate the DOM elements.

  2. Example Code:

  3. Run the file in a browser:

    • Open the HTML file in a browser and observe how the heading text changes immediately, and how clicking the button changes the paragraph text.


Explanation of the Code:

  • Accessing Elements: The script uses document.getElementById() to access the <h1>, <p>, and <button> elements.

  • Modifying Elements: The innerText property is used to change the content of the <h1> and <p> elements.

  • Event Handling: An event listener is added to the button, so when it is clicked, the paragraph text changes dynamically.


Challenge:

  • Modify the DOM Interactively:

    • Create a new button that changes the color of the heading text when clicked.

    Example:


Activity Follow-up Questions:

  1. What is the DOM, and how does it relate to the structure of an HTML page?

  2. How does JavaScript use the DOM to manipulate HTML elements?

  3. Can you think of a scenario where dynamically changing the content or style of a webpage might be useful?


Expected Outcome:

Students will:

  • Understand what the DOM is and how it represents HTML elements in JavaScript.

  • Practice selecting and manipulating DOM elements using JavaScript methods like getElementById().

  • Explore how dynamic web pages work by interacting with the DOM to change content and respond to user input (e.g., clicking buttons).

Last updated