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
Need the output of the inputs to be pushed into the <div>

I'm trying to get the user inputs to be pushed out as a message into a <div> via the button press after confirming that all the inputs are filled and the correct password has been entered. That part works fine but my forEach is not working:

If the user has input a number value lower than 18, I want an if that types out via push "Your name is UName and you are Age years old, you are not of legal age", or else the same result as above but ending with "...you are of legal age".

Any idea how I should go forth?

let nameElement = document.getElementById("UNameInput");
let faultElement = document.getElementById("errorOutput");
let ageElement = document.getElementById("AgeInput");
let clickElement = document.getElementById("button");
let passwordElement = document.getElementById("PasswordInput");
clickElement.addEventListener("click", function(e) {

  let feedback = [];
  if (UNameInput.value === '' || UNameInput.value === null) {
    feedback.push('Name input missing')
  }

  if (AgeInput.value === '' || AgeInput.value === null) {
    feedback.push('Age input missing')
  } else if (PasswordInput.value !== 'IHM') {
    feedback.push('Password is not valid')
  } else {
    feedback.forEach((item, i) => {
      if (item.AgeInput < 18) {
        feedback.push("You are not of not legal age")
      } else {
        feedback.push(" You are of legal age ")
      }
    });
  }
  if (feedback.length > 0) {
    e.preventDefault()
    faultElement.innerText = feedback.join(", ")
  }
});
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="JSny.js" defer></script>
</head>

<body>
  <div id="errorOutput"></div>
  <p id="output"></p>
  <div>
    <label for="UNameInput">Name Input</label>
    <input id="UNameInput" name="name" type="text">
  </div>
  <div>
    <label for="AgeInput">Age Input</label>
    <input id="AgeInput" name="age" type="number">
  </div>
  <div>
    <label for="PasswordInput">Password Input</label>
    <input id="PasswordInput" name="password" type="password">
  </div>
  <button id="button">Submit</button>
</body>
</html>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are using AgeInput instead of ageElement in the if statement.

about 4 years ago · Juan Pablo Isaza 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