Compare commits

3 Commits

Author SHA1 Message Date
Bruno Dalessandro
8a9b565164 Ejercicio 5 2026-05-27 15:41:49 -03:00
Bruno Dalessandro
248c29b43b Ejercicio 4 2026-05-25 08:03:19 -03:00
Bruno Dalessandro
aea030ff31 Ejercicio 3 2026-05-24 14:29:38 -03:00
3 changed files with 219 additions and 6 deletions

View File

@@ -29,7 +29,7 @@ boton.addEventListener("click", () => {
} }
}) });
/* /*
@@ -45,4 +45,175 @@ inputTexto.addEventListener("input", () => {
let cantidad = inputTexto.value.length; let cantidad = inputTexto.value.length;
document.getElementById("cantidadCaracteres").textContent = cantidad; document.getElementById("cantidadCaracteres").textContent = cantidad;
}) });
/*
3. Crear una lista con cinco ítems. Usando delegación de eventos, hacer que al
hacer clic en cualquier ítem se le agregue la clase `"seleccionado"` y se
la quite a los demás (es decir, solo un ítem puede estar seleccionado a la
vez).
*/
const lista3 = document.getElementById("lista-nueva");
lista3.addEventListener("click", (evento) => {
const items = document.querySelectorAll("#lista-nueva li");
for (const item of items) {
item.classList.remove("seleccionado");
}
evento.target.classList.add("seleccionado");
});
/*
4. Agregar a la página los siguientes elementos:
```html
<div id="externo">
<div id="interno">
<button>Click</button>
</div>
</div>
```
Registrar listeners de bubbling en los tres elementos y verificar en consola
el orden en que se ejecutan al hacer clic en el botón. Luego, agregar un
listener de capturing en `#externo` y observar cómo cambia el orden.
*/
// bubbling:
const externoBub = document.getElementById("externoBub");
const internoBub = document.getElementById("internoBub");
const botonBub = document.getElementById("botonBub");
externoBub.addEventListener("click", () => {
console.log("DIV EXTERNO");
});
internoBub.addEventListener("click", () => {
console.log("DIV INTERNO");
});
botonBub.addEventListener("click", () => {
console.log("BOTON");
});
// Capturing
const externoCap = document.getElementById("externoCap");
const internoCap = document.getElementById("internoCap");
const botonCap = document.getElementById("botonCap");
externoCap.addEventListener("click", () => {
console.log("DIV EXTERNO");
}, true);
internoCap.addEventListener("click", () => {
console.log("DIV INTERNO");
}, true);
botonCap.addEventListener("click", () => {
console.log("BOTON");
}, true);
/*
5. Crear un formulario con los campos nombre, edad y mensaje (textarea). Al
enviarlo:
- Verificar que ningún campo esté vacío.
- Verificar que la edad sea un número entero positivo, menor que 120.
- Si hay errores, mostrarlos en la página junto al campo correspondiente.
- Si todo es válido, mostrar un mensaje de éxito y limpiar el formulario.
Para limpiar un campo se puede asignar un string vacío a su propiedad
`value`:
```js
document.querySelector("#nombre").value = "";
```
*/
function validarNombre() {
const nombre = document.getElementById("nombre").value;
if (nombre.trim() === "") {
document.getElementById("errorNombre").textContent="El campo nombre no puede estar vacio";
return false;
}
document.getElementById("errorNombre").textContent = "";
return true;
}
function validarEdad() {
const edadTexto = document.getElementById("edad").value;
if (edadTexto.trim() === "") {
document.getElementById("errorEdad").textContent = "Escribir la edad";
return false;
}
const edad = parseInt(edadTexto);
if (isNaN(edad)) {
document.getElementById("errorEdad").textContent = "La edad no puede ser un texto";
return false;
}
if (edad <= 0 || edad >= 120) {
document.getElementById("errorEdad").textContent = "Edad invalida";
return false;
}
document.getElementById("errorEdad").textContent = "";
return true;
}
function validarMensaje () {
const mensaje = document.getElementById("mensaje").value;
if (mensaje.trim() === "") {
document.getElementById("errorMensaje").textContent = "El mensaje no puede estar vacio";
return false;
}
document.getElementById("errorMensaje").textContent = "";
return true;
}
const form = document.querySelector("#formulario")
form.addEventListener("submit", (evento) => {
evento.preventDefault();
const nombreValido = validarNombre();
const edadValida = validarEdad();
const mensajeValido = validarMensaje();
if (nombreValido && edadValida && mensajeValido) {
document.getElementById("exito").textContent = "Formulario enviado correctamente";
document.getElementById("nombre").value = "";
document.getElementById("edad").value = "";
document.getElementById("mensaje").value = "";
}
});

View File

@@ -3,3 +3,17 @@ div {
padding: 10px; padding: 10px;
margin: 10px; margin: 10px;
} }
.seleccionado {
background-color: lightblue;
}
/* Le agregue esto para que me aparezca el cursor sobre los items,
y para que no se pueda seleccionar todos los items como si fuera un texto. Eso hacia que queden todos seleccionados y me rompia el codigo. */
#lista-nueva li {
cursor: pointer;
user-select: none;
}

View File

@@ -23,16 +23,44 @@
</div> </div>
<hr> <hr>
<div id="ejercicio-3"> <div id="ejercicio-3">
<!-- Agregar acá el código HTML que haga falta para el ejercicio 3 --> <ul id="lista-nueva">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
<li>item 4</li>
<li>item 5</li>
</ul>
</div> </div>
<hr> <hr>
<div id="ejercicio-4"> <div id="ejercicio-4">
<!-- Agregar acá el código HTML que haga falta para el ejercicio 4 --> <div id="externoBub">
<div id="internoBub">
<button id="botonBub">Click Bubbling</button>
</div>
</div>
<div id="externoCap">
<div id="internoCap">
<button id="botonCap">Click Capturing</button>
</div>
</div>
</div> </div>
<hr> <hr>
<div id="ejercicio-5"> <div id="ejercicio-5">
<!-- Agregar acá el código HTML que haga falta para el ejercicio 5 --> <form id="formulario">
</div>
<input type="text" id="nombre" placeholder="Escribe tu nombre">
<p id="errorNombre"></p>
<br>
<input type="text" id="edad" placeholder="Escribe tu edad">
<p id="errorEdad"></p>
<br>
<textarea id="mensaje" placeholder="Escribe un mensaje"></textarea>
<p id="errorMensaje"></p>
<br>
<button type="submit">Enviar</button>
</form>
<p id="exito"></p>
<script src="clase-9.js"></script> <script src="clase-9.js"></script>
</body> </body>
</html> </html>