A good practice exercise for people who have learned DOM manipulation, worry not if you do not know DOM manipulation.
I have written this tutorial to teach the basics of DOM manipulation.
The app will be able to add a new task, tick a task, remove a task and, clear the task board.
We will need to create three files: index.
css, and app.
Let us then get into coding.
The HTML fileindex.
htmlThat is it for the HTML file, you should be familiar with the HTML syntax and easily understand this code.
what we have there is a form which will be used for inserting tasks, the taskboard div and a clear link for clearing the taskboard.
The CSS filestyle.
cssThat is it for the CSS file, you can even come up with your own styling and write down the link of your to-do list app in the responses section so that I can see all your different styling.
For now, you should have something like this:HTML and CSS only3.
Before we start, uncomment the display: none; in the taskboard class in the CSS file.
part of the index.
html file, commenting out the li elements3.
Adding a single taskapp.
js : adding a task into the listThe code above is responsible for adding a new task into the taskboard.
Firstly, we call the function loadEvent this function is responsible for grouping all the events that the app is going to listen.
Within the loadEvent function, we have the first event listener, that listens to the submission event on the form and calls the function submit which is responsible for first to prevent the default behavior of a form submission, we do not want to have the page updated.
And then it checks if the string entered is empty or not, if it is not empty it calls the functionaddTask with the string submitted as a parameter.
The addTask function is responsible for adding the tasks into the taskboard.
It first locates the ul element in the page, secondly creates a new li element, then it assigns and formats how the task should appear on the taskboard.
Lastly, it appends the new li element within the ul tag.
Clearing the taskboardIn the same file app.
js, let us add an event listener for the clear link.
js : adding the clearing list functionalitySo we are listening to an event of a click on the clear link which has the id of clear.
After this event the clearList function is called and set the ul innerHTML to an empty string, removing every previous HTML that was within the tag ul, removing every element in the list.
Ticking and Deleting single elementsThe code above allows us to tick or delete a task.
If you see within the ?.loadEventsfunction a third event listener was added, that is responsible to listen to the click event within the ul element.
You may ask now how will the code know when the user clicks on the delete button or on the tick button?.Well, the deleteOrTick function helps us with that.
it basically checks the class name of the element that was clicked, if it has the class name of delete it calls the deleteTask function, else it calls the tickTask function.
The deleteTask functionThe deleteTask function first gets the parent element of the target, which is the li element, then it gets the parent node of the li element which is the ul element.
lastly, it calls the removeChild on the parent element and takes as a parameter the li element selected, which is then removed from the DOM.
The tickTask functionThis function starts with const task = e.
nextSibling; this line of code gets the text that is within the li element, e.
target is the tick button, so e.
nextSibling is the actual task.
Then it checks if it was checked if it was it changes the text-decoration to line-through and change the text color to #ff0000 .
If it was unchecked it changes the text-decoration to none and changes back the text color to #2f44f .
ConclusionWell, that is it for the tutorial, now our app is fully functional.
You can download the code from here or see it live here.
Good day, good coding.