简体   繁体   中英

why is my script not printing text when i click the button

<!DOCTYPE html>
<html>
<head>
    <title>WhiteList</title>
</head>
 <body>
  <h1>Hello stranger lets see if you are on the list!!</h1>
  <input id="1" value="type here" placeholder="type here">
  <button onclick="click()">Check</button>
  <br>
  <p id=stuff></p>
  <script type="text/javascript">
    function click(){
      var name = document.getElementById('1').value;
      if (name == "tijmen"){
        document.getElementById("stuff").innerHTML = "<p>hey you are on the list welcome</p>"
      } else{
        document.getElementById("stuff").innerHTML = "<p>sorry you are not on the list</p>"
      }


    } 
  </script>
 </body>
</html>

so this is the code, there are no errors. but the problem is that the text won't print when i insert my name and click the button.... i realy cant seem to find the problem.

Problem here is click is defined as a click action so the engine is thinking you are calling the click of the button. Simple test shows what the browser sees.

 <button onclick="console.log(click)">Check</button> 

What can you do? Change the name, or even better, use addEventListener to bind the event listener.

Try add onclick to the JavaScript:

<body>
  <h1>Hello stranger lets see if you are on the list!!</h1>
  <input id="1" value="type here" placeholder="type here">
  <button id="btn">Check</button>
  <br>
  <p id=stuff></p>
  <script type="text/javascript">
   document.getElementById("btn").onclick = function() {
      var name = document.getElementById('1').value;
      if (name === "tijmen"){
        document.getElementById("stuff").innerHTML = "<p>hey you are on the list welcome</p>"
      } else {
        document.getElementById("stuff").innerHTML = "<p>sorry you are not on the list</p>"
      }
    } 
  </script>
</body>

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