简体   繁体   中英

Javascript conditional hiding and showing

Hello guys I'm confused with javascript code, I want a program that gets the input from the user, and if that input matches a specific value like 1234 I want it to hide part of the form. Eg

 var x=document.getElementById('pin').value; function hidden() { if (x.value=1234){ document.getElementById('pin').style.display="none"; } } 
 <input type="number" name="pin" placeholder="Please Enter Your Pin" id="pin"> <button onclick="hidden()">Enter</button> 

 var x=document.getElementById('pin'); function checkPin() { if (x.value == "1234"){ x.style.display="none"; } } 
 <input type="number" name="pin" placeholder="Please Enter Your Pin" id="pin" /> <button onclick="checkPin()">Enter</button> 

The value is not a native number, but a string, and you're assigning in the conditional check. Instead of '=' use '==' or '==='.

Try this:

function hidden() {
  var x = document.getElementById('pin').value;
  if (x === '1234'){
    document.getElementById('pin').style.display = 'none';
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM