forked from marquez.juan/clase-10-ejercicios-de-repaso
ejercicio2
This commit is contained in:
@@ -1 +1,21 @@
|
|||||||
// Agregar aquí el código javascript
|
// Agregar aquí el código javascript
|
||||||
|
|
||||||
|
// Crear una página con un campo de texto y un botón "Agregar". Cada vez que se
|
||||||
|
// haga clic en el botón, agregar el texto del campo como un nuevo ítem en una
|
||||||
|
// lista `<ul>`. Después de agregar el ítem, limpiar el campo.
|
||||||
|
|
||||||
|
// Si el campo está vacío al hacer clic, no agregar nada.
|
||||||
|
|
||||||
|
let input = document.querySelector("input") //almacenamos en variables el boton, el input y la lista
|
||||||
|
let boton = document.querySelector("button");
|
||||||
|
let lista = document.querySelector("ul")
|
||||||
|
|
||||||
|
boton.addEventListener("click", ()=>{ // creamos el listener "click" con la funcion
|
||||||
|
if (!(input.value.trim() === "")){ // si el input NO ESTÁ vacio, continuamos :
|
||||||
|
nuevoLi = document.createElement("li"); //creamos el li ,
|
||||||
|
nuevoLi.textContent = input.value.trim(); // le damos contenido ,
|
||||||
|
lista.appendChild(nuevoLi); // lo anexamos a la lista ,
|
||||||
|
input.value = ""; // y vaciamos el input :)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// fin
|
||||||
@@ -8,7 +8,13 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Ejercicio 2</h1>
|
<h1>Ejercicio 2</h1>
|
||||||
|
<p>escriba un texto para agregar a la lista</p>
|
||||||
|
<br>
|
||||||
|
<input type="text" name="input" id="input">
|
||||||
|
<button id="boton-agregar">Agregar</button>
|
||||||
|
<br>
|
||||||
|
<ul>
|
||||||
|
</ul>
|
||||||
<script src="ejercicio2.js"></script>
|
<script src="ejercicio2.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user