forked from BuildWithHussain/vtf-training
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25e9c92
commit 6240b9c
Showing
3 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters