forked from marquez.juan/clase-10-ejercicios-de-repaso
Ejercicio 2
This commit is contained in:
@@ -1 +1,29 @@
|
|||||||
// Agregar aquí el código javascript
|
// Agregar aquí el código javascript
|
||||||
|
// 1. Selección de los elementos del DOM mediante sus identificadores
|
||||||
|
const inputItem = document.getElementById("input-item");
|
||||||
|
const btnAgregar = document.getElementById("btn-agregar");
|
||||||
|
const listaContenedor = document.getElementById("lista-contenedor");
|
||||||
|
|
||||||
|
// 2. Registro del escuchador de eventos para el clic del botón
|
||||||
|
btnAgregar.addEventListener("click", function () {
|
||||||
|
|
||||||
|
// 3. Captura y limpieza de espacios en blanco del valor actual del input
|
||||||
|
const textoItem = inputItem.value.trim();
|
||||||
|
|
||||||
|
// 4. Validación: si la cadena está vacía, se interrumpe la ejecución
|
||||||
|
if (textoItem === "") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Creación de un nuevo elemento de lista (<li>) en memoria
|
||||||
|
const nuevoLi = document.createElement("li");
|
||||||
|
|
||||||
|
// 6. Asignación del texto validado al contenido del nuevo elemento
|
||||||
|
nuevoLi.textContent = textoItem;
|
||||||
|
|
||||||
|
// 7. Inserción del nuevo elemento como hijo del contenedor <ul>
|
||||||
|
listaContenedor.appendChild(nuevoLi);
|
||||||
|
|
||||||
|
// 8. Restablecimiento del campo de texto a una cadena vacía
|
||||||
|
inputItem.value = "";
|
||||||
|
});
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
<!DOCTYPE html>
|
<!doctype html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8" />
|
||||||
<meta name="viewport" content="width=device-width">
|
<meta name="viewport" content="width=device-width" />
|
||||||
<title>Ejercicio 2</title>
|
<title>Ejercicio 2</title>
|
||||||
<link rel="stylesheet" href="estilo.css">
|
<link rel="stylesheet" href="estilo.css" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Ejercicio 2</h1>
|
<h1>Ejercicio 2</h1>
|
||||||
|
<input type="text" id="input-item" placeholder="Escribe un nuevo ítem" />
|
||||||
|
<button id="btn-agregar">Agregar</button>
|
||||||
|
<ul id="lista-contenedor"></ul>
|
||||||
<script src="ejercicio2.js"></script>
|
<script src="ejercicio2.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user