WDD 131: Dynamic Web Fundamentals

JavaScript: DOM Manipulation

Overview

One of the key skill sets of any frontend web developer is the ability to manipulate the document dynamically using the DOM, a JavaScript object that is built after the browser initially builds the document. To manipulate the DOM means to read, edit, update, or delete the document, including CSS properties, dynamically. The DOM is the outline of the HTML and content nodes. The purpose of this activity is to introduce the HTML DOM and to learn how to change or manipulate the document using JavaScript.

Associated Course Learning Outcomes

Demonstrate proficiency with JavaScript language syntax.
Use JavaScript to respond to events and dynamically modify HTML.

Prepare

The JavaScript DOM explained in 5 minutes! - Bro Code

Activity Instructions

Example screen shot of Book of Mormon chapter application

This Book of Mormon application will be added upon in future learning activities. We start now with the interface and basic DOM assignments and build.

This app allows users to enter their favorite Book of Mormon chapters and display them on a list that is updated automatically on the screen. Entries can then be deleted from the displayed list.

  1. Create an HTML file named "bom.html" in the week02 folder.
  2. Your bom.html HTML document should include the basic meta tags and an appropriate title.
  3. Create an external CSS file and a JS file and place them in appropriate sub-folders within the week02 folder.
    Check Your Understanding

    In the week02 folder, create two folders named styles and scripts. Your css file will go in the styles and your js file will go in the scripts. You already have folders on the root wdd131 named styles and scripts but that is OK. We will treat this week02 folder independently of the outer wdd131 root folder.

  4. Copy and paste the basic interface (the HTML and CSS) from the following CodePen BOM Top 10.
  5. In your blank js file, declare three (3) variables that hold references to the input, button, and list elements.
    Check Your Understanding
            const input = document.querySelector('#favchap');
    const button = document.querySelector('button');
    const list = document.querySelector('______'); // you need to fill in the blank to reference the HTML element that is a unordered list element.
  6. Create a li element that will hold each entries chapter title and an associated delete button.
    Check Your Understanding
    const li = document.createElement('li');

    We identified this variable as li, however, that was just for simplicity.
    The variable identifier/name did not have to be named the same as the element being created.

  7. Create a delete button.
    Check Your Understanding
    const deleteButton = document.createElement('button');
  8. Populate the li element variable's textContent or innerHTML with the input value.
    Check Your Understanding
    li.textContent = input.value;

    textContent is preferred over innerHTML because it is more secure.
    However, if you need to include HTML tags, then you would use innerHTML.
    textContent will not render HTML tags. It will display the tags as text.

    Why is the value property used?
    Because the input variable references an HTML input text field and that is what is wanted, i.e., the user's entry. Here is the HTML that was provided: <input type="text" id="favchap" placeholder="Alma 5">

  9. Populate the button textContent with a ❌.
    Check Your Understanding
    deleteButton.textContent = '❌';
  10. Append the li element variable with the delete button.
    Check Your Understanding
    li.append(deleteButton);
  11. Append the li element variable to the unordered list in your HTML.
    Check Your Understanding
    list.append(li);

So far, we have just setup the interface and completed some basic DOM interaction. The application will not work at this point. The next activity will teach how to respond to events, like button clicks. We need to wait for the user to make an entry into the favorite chapter text field before processing.

Submission

Optional Resources