Adam Arnold
Adam Arnold

Reputation: 1

Javascript errors when calling

I have read and tried everything I can think of. The other pages that look identical with calling the function onclick work fine. I have tried all I know and read extensively with no avail.

<html>
<head>
    <title>Password</title>
</head>

    <body>

        <div id="output"></div>
        <input id="playername" placeholder = "Username" /> <br>
        <input id="password" placeholder = "Enter Password"/>
        <button onclick="test()">Are you correct?</button>
        <script src="pword.js"></script>    
    </body>

JS:

function test() {

    let output = document.querySelector("#output");
    let playername = document.querySelector("#playername");
    let password = document.querySelector("#password");

    if output.value === ("username") && password.value === ("Pa$$w0rd") {  
        console.log("CORRECT!");
    } else {
        console.log("Incorrect, try again");
    }

}

Upvotes: 0

Views: 45

Answers (4)

Kanishka Malhotra
Kanishka Malhotra

Reputation: 1

You are checking output.value instead of playername.value.

You can have a look at the working code here: https://repl.it/repls/ImaginaryCandidPerformance

Upvotes: 0

Taki
Taki

Reputation: 17654

You're checking output.value instead of playername.value and the parenthesis are misplaced, here's a snippet with fixed code :

function test() {
  let output = document.querySelector("#output");
  let playername = document.querySelector("#playername");
  let password = document.querySelector("#password");

  if (playername.value === "username" && password.value === "Pa$$w0rd") {
    console.log("CORRECT!");
  } else {
    console.log("Incorrect, try again");
  }
}
<div id="output"></div>
<input id="playername" placeholder="Username" /> <br>
<input id="password" placeholder="Enter Password" />
<button onclick="test()">Are you correct?</button>

Upvotes: 0

Enzo B.
Enzo B.

Reputation: 2371

You forgot a bracket during your if statement

if HERE => ( output.value === ("username") && password.value === ("Pa$$w0rd") ) <= AND HERE {  
    console.log("CORRECT!");
} else {
    console.log("Incorrect, try again");
}

And it's better to do something like this, remove you'r onclick on HTML and do this :

HTML :

<button id="MyButton">Are you correct?</button>

JS :

var MyBtn = document.getElementById("MyButton");
MyBtn.addEventListener("click", test);

Upvotes: 1

demkovych
demkovych

Reputation: 8827

if (playername.value === "username" && password.value === "Pa$$w0rd") {  
    console.log("CORRECT!");
} else {
    console.log("Incorrect, try again");
}

Upvotes: 0

Related Questions