Exercise Walkthrough: JS DOM
Exercise #1: SELECTING/MANIPULATING ELEMENTS#
Select Node #1 using the
getElementById()method, and change the text to: “I used the getElementById(“node1”) method to access this”Select Node #2 using the
getElementsByClassName()method, and change the text to: "I used the getElementByClassName("node2") method to access this"Select ALL the h3 tags using the
getElementsByTagName()method, and change the text to: "I used the getElementByTagName("h3") method to access all of these"
Exercise #2: CREATING/APPENDING/INSERTING ELEMENTS/OBJECTS#
Create a paragraph element using this
element.createElement()and put this text inside "This node was created using the createElement() method"Append the created node to the parent node using the
element.appendChild()method (Keep in mind that you’ll need to select the parent node first)Create a
<a>element using thiselement.createElement()and put this text inside "I am a<a>tag"Add a link
hrefto the<a>by selecting theanchorElement.linkpropertyInsert the created
<a>in the parent node, but before the<p>you just created using theelement.insertBefore()method
Exercise #3: REMOVING/REPLACING ELEMENTS/OBJECTS#
Replace the "Child Node" with a new
<p>element that reads "New Child Node" using thereplaceChild()method.Remove the "New Child Node" using the
remove()method orremoveChild()method.setTimeoutis used here to create a delayed action. TheremoveChildmethod will be called after 5000 milliseconds (5 seconds) to allow you to see both replace and remove actions being performed on the same element. This is purely for demonstration.
Exercise #4: ELEMENTS FROM AN ARRAY#
Use the following array of values to generate a list on the DOM
Create an unordered list element
Iterate over the array values, and create a list item element for each
Append the unordered list to the
div#containerunder exercise 4
Exercise #5: DOM EVENTS#
This exercise has many sub-steps that will be broken down in more detail.
Write a function called
showwhich creates a new div with an alerting message to the user with this message -> "Clicking the button triggers the onclick event, which calls the JS function show()... which alerts the user"- Define a function called
show - Select the button that triggers a popup modal.
- Add an event listener to the button that calls an event handler function,
show, on click
- Define a function called
This div should be a 'modal' that covers the main content on the screen
Create a div for the modal container
Create a div for the modal body
Create a heading element for the modal title
Create a paragraph element for the modal content
Create a button element for a modal close button
Add text content to the modal title, content, and button
Add an id attribute to the modal container with a value of "modal"
Add a class name attribute to the modal body with a value of "modal-card"
Append modal title, content and close button to the modal body
Append the modal body to the modal container
Append the modal container to the document body
BONUS: The modal popup should be able to be closed. Refactor for this functionality
Create a function called
handleEscCheck the
event.keyproperty to determine if the user pressed the "Escape" keyRemove the modal from the DOM if they did press the "Escape" key
Add an event listener to the window for a "keyup" event that will call
handleEscAdd an event listener to the modal container and close button that will remove the modal container and window keyup listener
Altogether the exercise code should resemble:
Exercise Walkthrough Video Part 1 Exercise Walkthrough Video Part 2