DOM + EVENTS + PROJECT – JavaScript Day 2

๐Ÿš€ DAY 2: DOM EVENTS PROJECTDOM EVENTS PROJECT

What is the DOM?

DOM (Document Object Model) = how JavaScript โ€œseesโ€ your HTML.

๐Ÿ‘‰ It lets you:

  • Select elements
  • Change content
  • Respond to user actions

๐Ÿ” 1. Selecting Elements

let title = document.getElementById("title");
console.log(title);

Other ways:

document.querySelector(".className");
document.querySelectorAll("p");

โœ๏ธ 2. Changing Content

document.getElementById(“title”).innerText = “New Text”;

document.getElementById("title").innerText = "New Text";

Change HTML:

element.innerHTML = "<b>Hello</b>";

๐ŸŽจ 3. Changing Styles

document.getElementById("title").style.color = "blue";

๐Ÿ–ฑ๏ธ 4. Events (User Interaction)

Example: Click

button.addEventListener("click", function() {
  alert("Button clicked!");
});

โŒจ๏ธ 5. Input Event

input.addEventListener("input", function() {
  console.log(input.value);
});

This is your first real-world app ๐Ÿ”ฅ

๐Ÿ“ฆ Features:

  • Add task
  • Display tasks
  • Delete task

๐Ÿ’ป FULL CODE

<!DOCTYPE html>
<html>
<head>
  <title>To-Do App</title>
</head>
<body>

<h2>To-Do List</h2>

<input id="taskInput" placeholder="Enter task">
<button id="addBtn">Add Task</button>

<ul id="taskList"></ul>

<script>
let input = document.getElementById("taskInput");
let button = document.getElementById("addBtn");
let list = document.getElementById("taskList");

button.addEventListener("click", function() {
  let task = input.value;

  if (task === "") return;

  let li = document.createElement("li");
  li.innerText = task;

  // Delete button
  let deleteBtn = document.createElement("button");
  deleteBtn.innerText = "Delete";

  deleteBtn.addEventListener("click", function() {
    li.remove();
  });

  li.appendChild(deleteBtn);
  list.appendChild(li);

  input.value = "";
});
</script>

</body>
</html>

๐ŸŽฏ YOUR TASK (VERY IMPORTANT)

Upgrade this app:

โœ… Add these features:

  1. โœ” Mark task as completed (click to strike through)
  2. โœ” Save tasks (use localStorage)
  3. โœ” Press Enter key to add task

๐Ÿ’ก Hints (Try yourself first!)

โœ” Strike-through:

li.style.textDecoration = "line-through";

โœ” Save to localStorage:

localStorage.setItem("tasks", JSON.stringify(array));

โœ” Get from localStorage:

JSON.parse(localStorage.getItem("tasks"));

What You Just Learned

  • DOM manipulation
  • Event handling
  • Dynamic elements
  • Real app logic

๐Ÿ‘‰ This is core JavaScript power

You may also like...

18 Responses

  1. Thanks , I have just been looking for info
    approximately this subject for ages and yours
    is the greatest I’ve found out till now. However,
    what concerning the bottom line? Are you positive about the source?

Leave a Reply

Your email address will not be published. Required fields are marked *