Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!