The Document Object Model (DOM) is a programming interface for web documents. When a web page is loaded, the browser creates a tree-like structure of the HTML elements, which JavaScript can use to dynamically interact with and update the page.
Key Concepts:
Example: Consider this HTML:
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1 id="main-title">Hello, World!</h1>
</body>
</html>
With JavaScript, you can change the text inside the <h1> tag dynamically:
document.getElementById("main-title").textContent = "Welcome to JavaScript!";
JavaScript provides several methods to select and interact with HTML elements:
let title = document.getElementById("main-title");
console.log(title.textContent); // Output: Hello, World!
let title = document.querySelector("#main-title");
console.log(title.textContent); // Output: Hello, World!
let items = document.querySelectorAll("li");
items.forEach(item => console.log(item.textContent));
Questions & Answers:
You can use JavaScript to update the content and styles of HTML elements dynamically.
let title = document.getElementById("main-title");
title.textContent = "JavaScript is Awesome!";
title.style.color = "blue";
title.style.fontSize = "36px";
Questions & Answers:
What is an Event?
An event is an action or occurrence detected by the browser, such as a click, keypress, or mouse hover.
Adding Event Listeners: You can respond to events using addEventListener( ).
Example:
<button id="my-button">Click Me</button>
<script>
let button = document.getElementById("my-button");
button.addEventListener("click", function() {
alert("Button clicked!");
});
</script>
Common Event Types:
Questions & Answers:
Objective:
Create a web page where clicking a button changes the background color randomly.
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Color Changer</title>
</head>
<body>
<h1>Click the Button to Change Background Color</h1>
<button id="color-button">Change Color</button>
<script src="script.js"></script>
</body>
</html>
JavaScript:
let button = document.getElementById("color-button");
button.addEventListener("click", function() {
let randomColor = `rgb(${Math.floor(Math.random() * 256)},
${Math.floor(Math.random() * 256)},
${Math.floor(Math.random() * 256)})`;
document.body.style.backgroundColor = randomColor;
});
Explanation:
Questions for Practice:
Objective:
Create a simple to-do list where users can add tasks and mark them as complete.
HTML:
<!DOCTYPE html>
<html>
<head>
<title>To-Do List</title>
</head>
<body>
<h1>To-Do List</h1>
<input id="task-input" type="text" placeholder="Enter a task">
<button id="add-button">Add Task</button>
<ul id="task-list"></ul>
<script src="script.js"></script>
</body>
</html>
JavaScript:
let addButton = document.getElementById("add-button");
let taskInput = document.getElementById("task-input");
let taskList = document.getElementById("task-list");
addButton.addEventListener("click", function() {
let taskText = taskInput.value;
if (taskText !== "") {
let newTask = document.createElement("li");
newTask.textContent = taskText;
newTask.addEventListener("click", function() {
newTask.style.textDecoration = "line-through";
});
taskList.appendChild(newTask);
taskInput.value = ""; // Clear the input
}
});
Explanation:
Questions for Practice:
Recap:
Next Steps:
Questions & Answers for Recap:
GoInnovateSoftware
Copyright © 2024 GoInnovateSoftware - All Rights Reserved.
Powered by GoDaddy
We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.