Skip to content

Commit

Permalink
todo code
Browse files Browse the repository at this point in the history
  • Loading branch information
NagariaHussain committed Jun 10, 2024
1 parent 25e9c92 commit 6240b9c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
21 changes: 21 additions & 0 deletions dom/vanilla-todo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>Vanilla ToDo</title>
<meta charset="UTF-8" />
</head>

<body>
<h2>Awesome ToDo</h2>
<form id="todo-form">
<label for="todo-desc">Description</label>
<input name="description" required id="todo-desc" type="text" />

<input type="submit" value="Create" />
</form>

<!-- TODOs -->
<ol id="todo-list"></ol>
<script src="./main.js"></script>
</body>
</html>
42 changes: 42 additions & 0 deletions dom/vanilla-todo/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// getting DOM nodes/elements
const todoForm = document.getElementById("todo-form");
const todoList = document.getElementById("todo-list");

const todos = [];

// handling todo creation
todoForm.addEventListener("submit", (e) => {
// prevent default submission behavior
e.preventDefault();

const formData = new FormData(todoForm);
const description = formData.get("description");

todos.push(description);
renderTodoList();

// clear the form
todoForm.reset();
});

function renderTodoList() {
let listHTML = "";

for (let i = 0; i < todos.length; i++) {
listHTML += `
<li>${todos[i]}
<button onclick="deleteTodo(${i})">🗑️</button>
</li>
`;
}

todoList.innerHTML = listHTML;
}

function deleteTodo(index) {
// remove todo from the Array
todos.splice(index, 1);

// re-render the list
renderTodoList();
}
2 changes: 1 addition & 1 deletion practice.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Note: Please complete the essential readings for this day before proceeding.

**Exercise 1**

Implement Edit todo functionality in our Vanilla JS counter. You can start with [`dom/vanilla-counter-end`](./dom/vanilla-counter-end) as the starting point.
Implement Edit todo functionality in our Vanilla JS counter. You can start with [`dom/vanilla-todo`](./dom/vanilla-todo) as the starting point.

**Exercise 2**

Expand Down

0 comments on commit 6240b9c

Please sign in to comment.