Compare commits
2 Commits
solucion-e
...
Repaso
| Author | SHA1 | Date | |
|---|---|---|---|
| 0a50af5868 | |||
| 8b5b25c46e |
@@ -1 +1,21 @@
|
|||||||
// Agregar aquí el código javascript
|
// Agregar aquí el código javascript
|
||||||
|
// 1. Selección de los elementos del DOM
|
||||||
|
const inputNombre = document.querySelector("#campo-nombre");
|
||||||
|
const boton = document.querySelector("#boton-saludo");
|
||||||
|
const resultado = document.querySelector("#mensaje-resultado");
|
||||||
|
|
||||||
|
// 2. Registro del Event Listener utilizando una función clásica
|
||||||
|
boton.addEventListener("click", function () {
|
||||||
|
|
||||||
|
// 3. Obtención y limpieza del valor del campo de texto
|
||||||
|
const nombre = inputNombre.value.trim();
|
||||||
|
|
||||||
|
// 4. Estructura condicional para validar el contenido
|
||||||
|
if (nombre === "") {
|
||||||
|
// Caso en que el campo esté vacío
|
||||||
|
resultado.textContent = "Por favor, ingresá tu nombre.";
|
||||||
|
} else {
|
||||||
|
// Caso en que contenga texto válido
|
||||||
|
resultado.textContent = `Hola, ${nombre}!`;
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -8,7 +8,9 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Ejercicio 1</h1>
|
<h1>Ejercicio 1</h1>
|
||||||
|
<input type="text" id="campo-nombre" placeholder="Ingresá tu nombre">
|
||||||
|
<button id="boton-saludo">Saludar</button>
|
||||||
|
<p id="mensaje-resultado"></p>
|
||||||
<script src="ejercicio1.js"></script>
|
<script src="ejercicio1.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
@@ -1,46 +1 @@
|
|||||||
const form = document.querySelector("#formulario");
|
// Agregar aquí el código javascript
|
||||||
|
|
||||||
function mostrarError(id, mensaje) {
|
|
||||||
document.querySelector(id).textContent = mensaje;
|
|
||||||
}
|
|
||||||
|
|
||||||
function limpiarError(id) {
|
|
||||||
document.querySelector(id).textContent = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
form.addEventListener("submit", (e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
|
|
||||||
const nombre = document.querySelector("#nombre").value.trim();
|
|
||||||
const edad = document.querySelector("#edad").value.trim();
|
|
||||||
const password = document.querySelector("#password").value;
|
|
||||||
|
|
||||||
// Limpiamos todos los errores antes de volver a validar.
|
|
||||||
limpiarError("#error-nombre");
|
|
||||||
limpiarError("#error-edad");
|
|
||||||
limpiarError("#error-password");
|
|
||||||
document.querySelector("#exito").textContent = "";
|
|
||||||
|
|
||||||
let valido = true;
|
|
||||||
|
|
||||||
if (nombre === "") {
|
|
||||||
mostrarError("#error-nombre", "El nombre no puede estar vacío.");
|
|
||||||
valido = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (edad === "" || parseInt(edad) < 0 || parseInt(edad) > 100) {
|
|
||||||
mostrarError("#error-edad", "La edad no tiene un formato válido.");
|
|
||||||
valido = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (password.length < 8) {
|
|
||||||
mostrarError("#error-password", "La contraseña debe tener al menos 8 caracteres.");
|
|
||||||
valido = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Solo mostramos el éxito si todos los campos son válidos.
|
|
||||||
if (valido) {
|
|
||||||
document.querySelector("#exito").textContent = "Formulario enviado correctamente.";
|
|
||||||
form.reset(); // reset() limpia todos los campos del formulario
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -1,6 +1,2 @@
|
|||||||
/* Agregar el código CSS necesario para el ejercicio */
|
/* Agregar el código CSS necesario para el ejercicio */
|
||||||
.error {
|
|
||||||
color: red;
|
|
||||||
font-size: 0.5em;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,22 +8,6 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Ejercicio 7</h1>
|
<h1>Ejercicio 7</h1>
|
||||||
<form id="formulario">
|
|
||||||
<div>
|
|
||||||
<input type="text" id="nombre" placeholder="Nombre">
|
|
||||||
<span class="error" id="error-nombre"></span>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<input type="number" id="edad" placeholder="Edad">
|
|
||||||
<span class="error" id="error-edad"></span>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<input type="password" id="password" placeholder="Contraseña">
|
|
||||||
<span class="error" id="error-password"></span>
|
|
||||||
</div>
|
|
||||||
<button type="submit">Enviar</button>
|
|
||||||
<p id="exito"></p>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<script src="ejercicio7.js"></script>
|
<script src="ejercicio7.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user