Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
¿Cómo guardo el contenido después del onclick?

Ya conecté el html con js y obtengo los datos de un formulario cuando se activa el onclick pero como guardo cada dato que recibo para usarlo después, como saco los datos de la función sin tener problemas:

 var getData = () => { let name = document.getElementById("name").value; let age = document.getElementById("age").value; if (name == "") { document.getElementById("name").focus(); } else { if (age == "") { document.getElementById("age").focus(); } else { console.log(name + " " + age); document.getElementById("name").value = ""; document.getElementById("age").value = ""; document.getElementById("name").focus(); } } };
about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

Debe crear una variable externa para guardar los datos que desea, cuando finaliza la función que está utilizando, los datos en ella "desaparecen". Entonces, para guardarlo, debe crear una variable externa.

De todos modos, debería ser bueno saber un poco más del problema al que te enfrentas.

about 4 years ago · Santiago Gelvez Denunciar

0

Algunas explicaciones en los comentarios del código:

 // Define checkGetData function const checkGetData = () => { // Read data from input const name = document.getElementById("name").value; const age = document.getElementById("age").value; if (name == "") { // If name is empty, focus on field document.getElementById("name").focus(); // Retuen false from getData function return false; } else if (age == "") { // Same with age document.getElementById("age").focus(); return false; } else { // If both field have value // Log it console.log(name + " " + age); // Reset inputs document.getElementById("name").value = ""; document.getElementById("age").value = ""; document.getElementById("name").focus(); } // Return variables 'name' and 'age' from // getData function back to caller. Values // are still there, now they do not rely on // input fields, because you first assign // values to this variables from input fields // and only after made this input fields empty. return { name, age }; }; // Define submitForm function const submitForm = () => { // Call checkGetData function // and set it's return value to new variable const inputData = checkGetData(); // Check inputData if (!inputData) { // If inputData false // show message in div document.getElementById("info").innerText = "Some fields are empty!"; } else { // If value exist, show // it in div document.getElementById("info").innerText = `Name: ${inputData.name}, Age: ${inputData.age}`; } // // So basically now you have input values in // inputData variable. You can pass it to any // other function, or return to parent caller // if it exist. Obviously this additional layer // with function submitForm is added here just // for reference and you can apply any logic // you prefer // }
 Name: <input id="name" type="text"></input> Age: <input id="age" type="number"></input> <button onClick="submitForm()">Submit</button> <br/> <br/> <div id="info" style="color:red"></div>

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda