-
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
Showing
1 changed file
with
62 additions
and
0 deletions.
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,62 @@ | ||
const taskInput = document.getElementById('taskInput'); | ||
const todoList = document.getElementById('todoList'); | ||
const completedList = document.getElementById('completedList'); | ||
const addTaskButton = document.getElementById('addTaskButton'); | ||
|
||
addTaskButton.addEventListener('click', addTask); | ||
|
||
function addTask() { | ||
const taskText = taskInput.value.trim(); | ||
|
||
if (taskText !== '') { | ||
const currentDate = new Date(); | ||
const dateString = currentDate.toLocaleDateString(); | ||
const timeString = currentDate.toLocaleTimeString(); | ||
|
||
const taskItem = document.createElement('li'); | ||
taskItem.innerHTML = ` | ||
<span>${taskText}</span> | ||
<span class="date">${dateString} ${timeString}</span> | ||
<button class="completeButton">Complete</button> | ||
<button class="editButton">Edit</button> | ||
<button class="deleteButton">Delete</button> | ||
`; | ||
|
||
todoList.appendChild(taskItem); | ||
taskInput.value = ''; | ||
} | ||
} | ||
|
||
// Add event delegation for dynamic buttons | ||
document.addEventListener('click', function (event) { | ||
const target = event.target; | ||
|
||
if (target.classList.contains('completeButton')) { | ||
completeTask(target); | ||
} else if (target.classList.contains('editButton')) { | ||
editTask(target); | ||
} else if (target.classList.contains('deleteButton')) { | ||
deleteTask(target); | ||
} | ||
}); | ||
|
||
function completeTask(button) { | ||
const taskItem = button.parentNode; | ||
taskItem.classList.toggle('completed'); | ||
completedList.appendChild(taskItem); | ||
} | ||
|
||
function editTask(button) { | ||
const taskItem = button.parentNode; | ||
const taskText = taskItem.querySelector('span'); | ||
const newText = prompt('Edit task:', taskText.textContent); | ||
|
||
if (newText !== null) { | ||
taskText.textContent = newText; | ||
} | ||
} | ||
|
||
function deleteTask(button) { | ||
const taskItem = button.parentNode; | ||
taskItem.parentNode.removeChild(taskItem); | ||
} |