Entendiendo el DOM
¿Para qué aprender JavaScript?
¿Qué es DOM y Window Object?
Explorando Nodos del DOM
Seleccionando y Accesando a Elementos del DOM
Consultando el DOM con getElementById y querySelector
Navegación efectiva entre nodos
Manipulando Elementos del DOM
Modificando atributos y propiedades
Modificando texto en HTML
Modificando estilos en HTML
Modificando la visibilidad de elementos HTML
Creación de elementos con HTML Strings e insertAdjacentElement()
Agregando elementos con innerHTML e insertAdjacentHTML()
Creación de elementos con createElement()
Remover elementos con remove() y removeChild()
Clonación y reemplazo de elementos con cloneNode y replaceChild
Manipulando Eventos en el DOM
Entendiendo eventos y tipos: Burbuja, Captura y Propagación
Agregar y eliminar escuchadores de eventos o Event Listeners
El objeto evento o eventObject
Manejo de entradas de formulario y validación
Delegación de eventos y prevención de comportamiento predeterminado
Creando un Administrador de Tareas
Añadir tareas desde un formulario
Eliminar y editar tareas del DOM
Almacenamiento y carga de datos en localStorage
Eliminar y editar tareas del almacenamiento local
Inspeccionando y solucionando errores en el código
Personalizando la interfaz del administrador de tareas
Aún hay más por aprender
Asincronía en JavaScript
¿Cómo funciona el JavaScript Engine?
You don't have access to this class
Keep learning! Join and start boosting your career
localStorage
using JavaScript?Efficient data management is crucial in any modern application. In this case, we will explore how to store and manage tasks in the browser's localStorage
using JavaScript, allowing modifications to persist even when refreshing the page. Ready to learn how to maintain the state of your tasks using JavaScript? Let's take it step by step.
We start by saving our tasks in the localStorage
. This ensures that even when you refresh the page, the record of the tasks already created is maintained. You can check the stored status of the tasks using the browser:
Accessing the localStorage
: You can view the stored tasks by inspecting the localStorage
through the developer console.
Storage structure: Tasks are stored in an array under a specific key, such as tasks
, for easy access.
Visual Feedback: Although we can delete and edit tasks from the user interface, without updating the localStorage
, these changes are not permanent. We need to address this update to maintain memory across page refreshes.
localStorage
update after editing or deleting tasks?To make the changes permanent when updating, we need to implement functions that reflect these modifications in the localStorage
. Here is how to do it:
LocalStorage
update function.The updateLocalStorage
function is called each time a task is edited or deleted and requires no parameters as it acts on the current state of the tasks.
function updateLocalStorage() { const taskElements = document.querySelectorAll('#task-list li'); const tasksArray = Array.from(taskElements).map(el => el.firstChild.textContent); localStorage.setItem('tasks', JSON.stringify(tasksArray));}
Retrieve Updated Content: we use querySelectorAll
to get all <li>
items from the task container, converting them to an array for manipulation.
Mapping and Serialization: Next, we map these nodes to retrieve their text content, achieving an array of updated tasks.
Storage: Finally, the array is serialized to a JSON string, storing it in the localStorage
under the tasks
key.
Integrate the updateLocalStorage
function within the task edit flow to ensure that every change is reflected in the localStorage
.
function editTask(taskIndex, newContent) { // Assume the functionality exists to make the changes in the UI document.querySelectorAll('#task-list li')[taskIndex].firstChild.textContent = newContent; updateLocalStorage();}
When working with a NodeList
(the structure returned by querySelectorAll
), converting it to an array is crucial to take advantage of array methods, such as map
, that are not available in a NodeList
.
Manipulation Flexibility: Using Array.from
allows to quickly convert a NodeList
to an array, providing access to methods such as map
, filter
and reduce
.
Ease of Iteration and Mapping: Once in array format, iterating and mapping to transform data becomes simple and effective.
Using localStorage
correctly, along with NodeList
conversion and manipulation, allows state to be managed persistently in web applications. This ensures that operations in the user interface, such as adding, editing and deleting tasks, are automatically reflected in the stored state, taking the user experience to a new level of reliability and consistency. Don't underestimate the importance of the key-value storage capability that localStorage
offers - it's a valuable resource in modern web development!
Keep exploring and honing your programming skills! Each tweak and new integrated feature is another step towards building more robust and complete applications.
Contributions 4
Questions 1
Want to see more contributions, questions and answers from the community?