16.6 Homework Assignment
Task:
Example Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Web Page</title>
<style>
body {
font-family: Arial, sans-serif;
}
#heading {
color: darkblue;
}
.content {
font-size: 16px;
color: black;
}
button {
margin: 10px;
padding: 10px;
}
</style>
</head>
<body>
<h1 id="heading">Welcome to My Interactive Web Page</h1>
<p id="paragraph" class="content">This paragraph will change dynamically when you interact with the page.</p>
<button id="changeTextButton">Change Text</button>
<button id="changeStyleButton">Change Style</button>
<button id="changeBackgroundButton">Change Background Color</button>
<input type="text" id="inputField" placeholder="Type something..." />
<p id="displayText"></p>
<script>
// Change text content when the button is clicked
let changeTextButton = document.getElementById("changeTextButton");
let heading = document.getElementById("heading");
let paragraph = document.getElementById("paragraph");
changeTextButton.addEventListener("click", function() {
heading.innerText = "Text Changed!";
paragraph.innerText = "This paragraph has been updated.";
});
// Change styles dynamically when the button is clicked
let changeStyleButton = document.getElementById("changeStyleButton");
changeStyleButton.addEventListener("click", function() {
heading.style.color = "red";
heading.style.fontSize = "30px";
paragraph.style.color = "green";
paragraph.style.fontStyle = "italic";
});
// Change background color of the page
let changeBackgroundButton = document.getElementById("changeBackgroundButton");
changeBackgroundButton.addEventListener("click", function() {
document.body.style.backgroundColor = "lightcoral";
});
// Update text content based on user input
let inputField = document.getElementById("inputField");
let displayText = document.getElementById("displayText");
inputField.addEventListener("input", function() {
displayText.innerText = "You typed: " + inputField.value;
});
// Add mouseover and mouseout effects to the heading
heading.addEventListener("mouseover", function() {
heading.style.textDecoration = "underline";
});
heading.addEventListener("mouseout", function() {
heading.style.textDecoration = "none";
});
</script>
</body>
</html>Explanation:
Challenge:
Homework Submission:
Expected Outcome:
Last updated