0

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");
    }

}
4
  • And what is the error? Can you provide a stack trace? The error message? Or at least the type of error? Commented Sep 20, 2019 at 15:25
  • 3
    The if statement in your test() function is incorrect; it's missing the parentheses around the expression. Commented Sep 20, 2019 at 15:25
  • I am sorry, this is the error ReferenceError: test is not defined Commented Sep 20, 2019 at 15:27
  • Perhaps a silly question, but Is the JS included on the page with the HTML? Commented Sep 20, 2019 at 18:27

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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>

1 Comment

Wow, thank you all so much for the help, it will help me see mistakes to hopefully not make in the future!
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.