forEach loop does not check all the items in the array if a condition is met in any of them

I have a forEach loop for an array but with an if statement to check if the input is empty. The problem is that the loop stops if it finds an empty input i want it to check all the element in the array even if 1 is empty

here is my code

function showError(input, message) {
  const formControl = input.parentElement;
  formControl.className = "form-control error";
  const small = formControl.document.querySelector("small");
  small.inerText = message;
}
//show success message
function showSuccess(input) {
  const formControl = input.parentElement;
  formControl.className = "form-control success";
}

//check requiered fields

function checkRequired(inputArr) {
  inputArr.forEach(function(input) {

    if (input.value.trim() === "") {
      showError(input, "is required");
    } else {
      showSuccess(input);
    }
  });
}

// Event listener
form.addEventListener("submit", function(e) {
  e.preventDefault();
  checkRequired([username, email, password, password2]);
});

enter image description here]

Upvotes: 0

Views: 321

Answers (1)

pope_maverick
pope_maverick

Reputation: 980

you misspelled innerText as inerText(so as your submit button name). start from there. there are many places in your code where an exception could happen, such as checkRequired. my suggestion is to start with

small.innerText = message;

Upvotes: 1

Related Questions