Manipulación Del DOM Con JavaScript
Manipulación Del DOM Con JavaScript
Introducción
El Modelo de Objetos de Documento (DOM) representa la estructura de un documento HTML
como un objeto, lo que permite interactuar con el contenido, la estructura y los estilos de las
páginas web y modificarlos dinámicamente. JavaScript proporciona diversos métodos de
manipulación del DOM que permiten a los desarrolladores crear páginas web dinámicas e
interactivas. En este laboratorio, aprenderá a manipular el DOM con JavaScript para crear una
aplicación de lista de tareas.
Objetivos
Al finalizar este laboratorio, usted:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To-Do List</title>
<style>
/* Styles the main container of the to-do list */
.todo-container {
width: 300px;
margin: 50px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
1. Estructura HTML:
La sección de script contendrá la lógica para interactuar con el DOM, como agregar
tareas, eliminar tareas y editar tareas.
Iniciar aplicación
When the user types a task into the input field and clicks the Add Task button, the task
will be added to the list.
Each task will be displayed with Edit and Delete buttons next to it.
1. Selecting elements:
Use document.getElementById('taskInput') to select the input field where the user will
type their task
Use document.getElementById('todoList') to select the unordered list (<ul>) where the
new tasks will be appended
Use document.createElement("li") to create a new list item (<li>) to hold the task
Use document.createElement("span") to create a span element that will hold the task
text
Create buttons for Edit and Delete using document.createElement("button")
Link the Edit button to an event handler by setting the onclick property to a
function (e.g., editTask(span))
Similarly, link the Delete button to an event handler (e.g., removeTask(li))
Append the task text and the buttons to the list item (<li>) using the appendChild()
method
Append the list item (<li>) to the unordered list (<ul>) using appendChild()
After adding the task to the list, clear the input field using input.value = ""
Output:
Reload the webpage to test the functionality. If the live server is not running, start it and
then launch the application.
Once the page is loaded, type a task in the input field and click Add Task. The task will be
added to the to-do list, appearing along with Edit and Delete buttons for each task.
Exercise 3: Editing a task in the To-Do List
In this exercise, you will implement the functionality to edit an existing task in the list. Here's
how it works:
When the user clicks the Edit button next to a task, they will be prompted to enter a new
task text.
If the user enters a valid new task text (non-empty), the task will be updated dynamically
in the list.
The task text will only be updated if the user provides a non-empty input.
Create a function editTask(span) where span refers to the element containing the
current task text
This function will allow you to prompt the user for new text to replace the current
task
Inside the editTask() function, use the prompt() method to ask the user for a new
task text. You can pass the current task (span.textContent) as the default value in the
prompt.
After the user enters their new task, check if the input is not null and not empty. Use
newTask.trim() to ensure that the task text is not just whitespace.
4. Update the task:
If the user provides valid input, update the task by assigning the new value to
span.textContent. This will dynamically update the task in the list.
Output:
Reload the webpage to test the functionality. If the live server is not running, start it and
then launch the application.
Once the page is loaded, clicking on the Edit button for a specific task will display a
prompt to update the task. Make your edits and click OK to save the changes.
When the user clicks the Delete button next to a task, the task will be removed from the
list.
The task will be removed dynamically from the unordered list.
When the user clicks the Delete button, use the removeChild() method to remove the
specific task (represented as an <li> element) from the list.
Pass the task (the <li> element) to the removeChild() method to remove it from the
DOM
Output:
Reload the webpage to test the functionality. If the live server is not running, start it and
then launch the application.
Once the page is loaded, clicking the Delete button will remove the corresponding task
from the list.
Conclusion
In this lab, you explored key DOM manipulation techniques to dynamically interact with a web
page. By completing the exercises, you gained practical experience with methods such as
createElement(), appendChild(), and removeChild(). These techniques are essential for building
interactive and user-friendly web applications, empowering you to create dynamic content
that responds to user interactions.
Author
Rajashree Patil