Creating a simple task management application using HTML and JavaScript allows users to add and delete tasks dynamically. Below is a step-by-step guide to building such an application:
1. HTML Structure:
Begin by setting up the HTML structure, which includes an input field for new tasks, an "Add" button, and a container to display the list of tasks.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<title>Task Management App</title>
<style>
/* CSS styles will be added here */
</style>
</head>
<body>
<div class="container">
<h1>Task Management</h1>
<div class="input-section">
<input id="taskInput" placeholder="Enter a new task" type="text"/>
<button id="addTaskBtn">Add Task</button>
</div>
<ul id="taskList">
<!-- Tasks will be appended here -->
</ul>
</div>
<script>
// JavaScript code will be added here
</script>
</body>
</html>
2. CSS Styling:
Add CSS to style the application for better user experience.
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
}
h1 {
text-align: center;
color: #333;
}
.input-section {
display: flex;
justify-content: space-between;
}
input[type="text"] {
flex: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
padding: 10px;
border: none;
background: #28a745;
color: #fff;
border-radius: 4px;
cursor: pointer;
margin-left: 10px;
}
button:hover {
background: #218838;
}
ul {
list-style: none;
padding: 0;
margin-top: 20px;
}
li {
background: #f9f9f9;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
.delete-btn {
background: #dc3545;
color: #fff;
border: none;
padding: 5px 10px;
border-radius: 4px;
cursor: pointer;
}
.delete-btn:hover {
background: #c82333;
}
</style>
3. JavaScript Functionality:
Implement JavaScript to handle adding and deleting tasks.
<script>
document.addEventListener('DOMContentLoaded', () => {
const taskInput = document.getElementById('taskInput');
const addTaskBtn = document.getElementById('addTaskBtn');
const taskList = document.getElementById('taskList');
// Function to add a new task
const addTask = () => {
const taskText = taskInput.value.trim();
if (taskText === '') {
alert('Please enter a task.');
return;
}
// Create list item
const li = document.createElement('li');
li.textContent = taskText;
// Create delete button
const deleteBtn = document.createElement('button');
deleteBtn.textContent = 'Delete';
deleteBtn.classList.add('delete-btn');
deleteBtn.onclick = () => {
taskList.removeChild(li);
};
// Append delete button to list item
li.appendChild(deleteBtn);
// Append list item to task list
taskList.appendChild(li);
// Clear input field
taskInput.value = '';
};
// Event listener for add task button
addTaskBtn.addEventListener('click', addTask);
// Allow pressing 'Enter' to add task
taskInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
addTask();
}
});
});
</script>
Explanation:
-
HTML: Sets up the structure with an input field, an "Add Task" button, and an unordered list to display tasks.
-
CSS: Provides styling for the application, including layout, colors, and button styles.
-
JavaScript:
- Listens for the DOM content to be fully loaded before executing scripts.
- Defines the
addTask function to create a new list item (li) with the task text and a delete button.
- Adds event listeners to the "Add Task" button and the input field to handle adding tasks via button click or pressing 'Enter'.
- The delete button, when clicked, removes the corresponding task from the list.
This simple task management application allows users to add tasks by entering text and clicking the "Add Task" button or pressing 'Enter'. Each task is displayed with a "Delete" button, enabling users to remove tasks as needed.
For more advanced features like editing tasks, marking them as completed, or persisting tasks using local storage, you can refer to comprehensive tutorials such as the one provided by GeeksforGeeks[1].