Practice and reinforce the concepts from Lesson 9
DO NOT DELETE the existing files in the template:
ONLY EDIT the necessary files.
⏱️ Estimated time: 15 minutes
What To Do In the index.html file, you'll see an unordered list with Coffee and Tea as list items. Your task is to add another list item called Milk to the current unordered list using DOM manipulation.
Steps:
ul
and select the "ul" element using document.querySelector
newli
and create a new "li" element using document.createElement
textContent
propertyappendChild
method:bulb: Tip Use
document.querySelector("ul")
to select the unordered list element. This selector will find the first<ul>
element in your document.
Expected Result: If you have done it correctly, your webpage should display a list with three items: Coffee, Tea, and Milk.
⏱️ Estimated time: 10 minutes
What To Do
Remove the paragraph element using the .remove()
method.
Steps:
p
and select the paragraph that you want to remove using document.querySelector
.remove()
method:bulb: Tip Use CSS selectors to target specific paragraphs:
document.querySelector(".classname")
document.querySelector("#idname")
document.querySelector("p")
Take a look at the HTML and see what kind of ways you could select the paragraph that you want to remove. The selector is based on CSS selector syntax.
Expected Result: If you have done it correctly, the targeted paragraph should be removed from the page.
⏱️ Estimated time: 20 minutes
What To Do Create a multiplication calculator that calculates the product of two numbers when the calculate button is pressed and displays the result as a paragraph below the button.
Steps:
div
and select it with the id "calc"num1
and num2
. Select them with their respective idscalculate()
for the button onclick event:
answer
and calculate the value using: num1.value * num2.value
newAnswer
and create a "p" element using document.createElement
num1.value + " x " + num2.value + " = " + answer
appendChild
:bulb: Tip Common Challenges:
.classList.add()
to add multiple classes to an element.value
to get the current value from input fieldsonclick=""
attribute is an inline JavaScript event handler that runs a function when the button is clickedLearn more about onclick: https://www.w3schools.com/jsref/event_onclick.asp
Expected Results:
When you click the calculate button with first number = 3 and second number = 4:
The answer should appear below the calculate button.
When you click the calculate button again with first number = 2 and second number = 2:
The next answer should appear below the previous answer, creating a history of calculations.
Make sure your DOM projects include:
Exercise 1 Requirements:
document.querySelector()
document.createElement()
textContent
appendChild()
Exercise 2 Requirements:
.remove()
methodExercise 3 Requirements:
.value
.classList.add()
:bulb: Tip Common Issues and Solutions:
Element not found: Make sure your script runs after the DOM is loaded. Place your script at the bottom of the body or use DOMContentLoaded
event.
Values showing as NaN: Remember to convert input values to numbers using parseInt()
or parseFloat()
if needed.
Classes not applying: Use .classList.add()
for each class separately or pass multiple arguments: .classList.add("class1", "class2")
Nothing happens on button click: Check that your function name in the onclick attribute matches your JavaScript function exactly.
:warning: Warning Before submitting, make sure to:
- Test all three exercises thoroughly
- Verify that all DOM manipulations work correctly
- Check that your calculator displays results properly
- Ensure no console errors appear
When you have completed all DOM exercises, submit your projects using the link below:
:information_source: Submit Your Projects Here Total estimated time: 45 minutes (15 + 10 + 20 minutes)