Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

todo list solution #274

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions todo.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Style for the task list */
ul {
list-style-type: none;
padding: 0;
}

li {
padding: 10px;
margin: 5px 0;
background-color: #f4f4f4;
border: 1px solid #ddd;
border-radius: 5px;
display: flex;
justify-content: space-between;
align-items: center;
}

/* Completed task style */
.complete {
text-decoration: line-through;
color: grey;
}

/*Delete button style*/

.deleteButton {
background-color: red;
color: white;
border: none;
border-radius: 50%;
padding: 5px 10px;
font-weight: bold;
}

17 changes: 17 additions & 0 deletions todo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To-Do List</title>
<link rel="stylesheet" href="todo.css">
</head>
<body>
<h1>To-Do List</h1>
<input type="text" id="taskInput" placeholder="Add new task...">
<button id="addTaskButton">Add Task</button>
<button id="clearAllButton">Clear All</button>
<ul id="taskManager"></ul>
<script src="todo.js" defer></script>
</body>
</html>
63 changes: 63 additions & 0 deletions todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//function to clear all task items
function clearAllTasks() {
let taskManager = document.getElementById('taskManager');
taskManager.innerHTML = ""; // This will remove all list items from the task list
}

//event listener for button click to clear all task items
document.getElementById('clearAllButton').addEventListener('click', clearAllTasks);

//DOM fully loaded before adding listeners
document.addEventListener("DOMContentLoaded", function() {

//event listener for button click
document.getElementById('addTaskButton').addEventListener('click', addNewTask);

//function to add a new task to the list
function addNewTask() {
let taskInput = document.getElementById('taskInput'); // Get the input element by the updated ID
let taskText = taskInput.value.trim(); // Get the input value and remove extra spaces

//if input is empty, do nothing
if (taskText === "") {
return; // Don't do anything if the input is empty
}

//create new list item (li)
let li = document.createElement("li");
li.textContent = taskText; // Set the text of the li element

//create 'X' button
let deleteButton = document.createElement("button");
deleteButton.textContent = "x";
deleteButton.classList.add("deleteButton"); // Add a class for styling

//add event listener for 'x' button to remove task item
deleteButton.addEventListener('click', function() {
li.remove(); // Remove the list item when the 'X' is clicked
});

//append 'x' button to the list item
li.appendChild(deleteButton);

//add an event listener to toggle 'complete' class when item is selected
li.onclick = function () {
this.classList.toggle("complete"); // Toggle 'complete' and strikethrough
};

//append new task to task manager list
document.getElementById("taskManager").appendChild(li);

//clear input field after adding the task
taskInput.value = "";
}
});