0

I’m having a problem where I want to get all elements in an array at once with one line of code. The problem is, I’m using .push to add extra elements. I want to select all existing and input elements by user. Here is an example.

let pass = [
    "example",
    "wow"
  ];

var message3 = document.getElementById("signup");

document.getElementById("myButton3").addEventListener("click", function() {
  pass.push(message3.value);
  alert(pass)
});

var message4 = document.getElementById('password');

document.getElementById("myButton4").addEventListener("click", function() {
  if (message4.value == pass) { // Here.
    alert('you pass!')
  }
  else {
    alert('wrong')
  }
});
<input type="text" id="signup" value="">
<br>
<button id="myButton3" type="button">Create password</button>
<br>
<input type="text" id="password" value="">
<br>
<button id="myButton4" type="button">Enter password</button>
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
MusaYasin
  • 17
  • 2
  • 3
    `if(message4.value == pass)` a string value will never be equal to an array. What are you trying to do here? – James Oct 06 '21 at 16:28
  • 2
    Are you looking for `pass.includes(message4.value)`? By _“without numbering them”_ did you mean “without iterating over the array”? Not sure how “numbering” array elements helps. They’re already indexed… – Sebastian Simon Oct 06 '21 at 16:29

0 Answers0