Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created November 3, 2022 20:23
Show Gist options
  • Save parzibyte/e9cb60b631a787d12dc500278a7eeca7 to your computer and use it in GitHub Desktop.
Save parzibyte/e9cb60b631a787d12dc500278a7eeca7 to your computer and use it in GitHub Desktop.
document.addEventListener("DOMContentLoaded", () => {
const $formulario = document.querySelector("#formulario");
const $notaTaller1 = document.querySelector("#notaTaller1"),
$notaTaller2 = document.querySelector("#notaTaller2"),
$notaParcial = document.querySelector("#notaParcial");
$formulario.onsubmit = function (evento) {
// Prevenir envío de formulario
evento.preventDefault();
// Recorrer elementos input y si alguno está vacío detenernos
const elementos = $formulario.elements;
for (const elemento of elementos) {
if (elemento.tagName === "INPUT" && elemento.value === "") {
return;
}
}
// Hasta aquí todos los campos están llenos
for (const $nota of [$notaTaller1, $notaTaller2, $notaParcial]) {
const valor = parseFloat($nota.value);
if (valor < 0 || valor > 10) {
return alert(`El valor de ${$nota.placeholder} debe estar entre 0 y 10`);
}
}
// Hasta aquí las notas son correctas
const datos = {};
for (const elemento of elementos) {
if (elemento.tagName === "INPUT") {
let valor = elemento.value;
if (elemento.type === "number") {
valor = parseFloat(elemento.value);
}
datos[elemento.id] = valor;
}
}
sessionStorage.setItem("datosEstudiante", JSON.stringify(datos));
window.location.href = "./imprimir.html";
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment