Reordering Todo Items
Lesson Overview
In this lesson, we will explore how to dynamically reorder items in a to-do list using JavaScript. We will cover the following topics:
- Understanding the children property: Learn how to access child elements of a parent list (HTML
ultag). - Converting HTML Collections into Arrays: Convert HTMLCollections into JavaScript arrays to leverage array methods.
- Finding the Index of a List Item: Use the
indexOfmethod on arrays to find the position of a list item. - Using the insertBefore function: Learn how to move items within a list using the
insertBeforemethod. - Combining Concepts: Reordering with Buttons: Implement buttons to move items up and down within the list.
Understanding the children property
Every HTML element on a web page is part of the Document Object Model (DOM), which means it can be thought of as an object. These objects can have child objects as well. So, if we have a simple <ul> HTML tag with several <li> tags inside, we can say that the <ul> tag is the parent, and the <li> tags are its children.
As we're dealing with a dynamic to-do list in our example, the <ul> tag represents our to-do list. Each <li> tag inside it is a to-do item on our list. In JavaScript, we can access these child objects using the children property. The children property is a way to access all the child elements of a specific parent element, in this case, our to-do items within the to-do list.
Here's how you can use it:
With this code, we first get the todo-list element using document.getElementById('todo-list') and store all its child elements (our to-do items) in the todoItems variable using the children property. This way, you can inspect and use information about your to-do items directly in your JavaScript code.
For example, if todo-list contains three to-do items, todoItems will be an HTMLCollection containing these three <li> elements. You can then manipulate these child elements using various JavaScript methods.
Converting HTML Collections into Arrays
Now that we can access the children of todoList, let's convert them into an array to use powerful array methods. Even though the children property gives us a list-like collection of our to-do items, it's not a real JavaScript array. It's an HTMLCollection, and while it might look like an array, it doesn’t have all the methods that a true JavaScript array has. For example, you can't use the indexOf or forEach methods on an HTMLCollection.
Think of Array.from like converting a guest list from a handwritten sheet (HTMLCollection) to a digital spreadsheet (Array). Now, you can sort, filter, and analyze the list easily. With Array.from(todoList.children), we can convert our HTMLCollection into a real JavaScript array. This allows us to use all the powerful methods an array has.
Here's a small example:
In this example, forEach() is used to iterate over each todoItem in the todoArray. The operation being performed is setting the text of each todo item to "Task " followed by its index in the array, effectively renaming each item in the list.
