Creating a basic JavaScript project is a great way to learn and apply your skills. Here's a step-by-step guide to help you build a simple yet functional JavaScript project. We'll create a To-Do List App, which is a common beginner project that covers important aspects of JavaScript like DOM manipulation, event handling, and basic functionality.
Project: To-Do List App
Project Overview
This simple to-do list app allows users to:
- Add new tasks.
- Mark tasks as complete.
- Delete tasks.
- Store tasks in local storage so they persist when the page is reloaded.
Step 1: Set up the Project Structure
Create a folder for your project, for example: todo-app
.
Inside the todo-app
folder, create the following files:
index.html
— the HTML structure.styles.css
— the CSS for styling the app.script.js
— the JavaScript code to make the app interactive.
Your project structure should look like this:
Step 2: HTML Structure (index.html
)
This is where you'll define the structure of your to-do list app, including an input box for entering new tasks and a list to display them.
Explanation:
- We have a title (
<h1>
) for the app. - An input field (
<input>
) for the user to type new tasks. - A button (
<button>
) that adds the task to the list when clicked. - An unordered list (
<ul>
) where the tasks will appear. - The JavaScript file is linked at the bottom (
script.js
).
Step 3: Styling the App (styles.css
)
Now, let's add some basic styles to make our to-do list look neat.
Explanation:
- The body is styled to center the app on the screen.
- The
.todo-container
class adds padding and a shadow to the container. - The input and button elements are styled to be user-friendly.
- The list items (
li
) have spacing, a bottom border, and a hover effect. - Tasks that are marked as "complete" have a line-through and lighter text color.
- A delete button (
.delete-btn
) is styled with red color and a hover effect.
Step 4: JavaScript Functionality (script.js
)
Now, let's add the functionality to make our to-do list interactive.
Explanation:
Adding a Task:
- The
addTask
function creates a new list item (li
) with the task text and a delete button. - When the delete button is clicked, the task is removed from the list and the tasks are saved in local storage.
- When a task is clicked, it toggles the
complete
class (line-through effect), and the tasks are saved again.
- The
Saving and Loading Tasks:
- Tasks are stored in the browser’s
localStorage
so they persist across page reloads. - The
saveTasks
function saves the list of tasks in the browser's local storage as a JSON string. - The
loadTasks
function loads tasks from local storage when the page is reloaded and creates list items based on the saved data.
- Tasks are stored in the browser’s
Step 5: Test Your App
- Open
index.html
in your browser. - Try adding new tasks by typing in the input field and clicking "Add Task."
- Mark tasks as complete by clicking on them.
- Delete tasks by clicking the "Delete" button.
- Refresh the page to make sure the tasks persist (you’ll see tasks that were added, completed, or deleted).
Output
Summary
You’ve now created a simple To-Do List App using HTML, CSS, and JavaScript. The app:
- Allows users to add, mark as complete, and delete tasks.
- Stores tasks in local storage so they persist even after a page reload.
You can expand on this project by adding more features like:
- Editing tasks.
- Sorting tasks.
- Adding categories or tags to tasks.
This is a great starting point for learning JavaScript and creating more interactive applications