0

So I am doing a small project and cannot seem to get this function to work. any idea what I am doing wrong?

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="script.js"></script>
    <title>Login</title>
</head>

<body>
    <form name="loginForm">
        <label>Password:</label> 
        <input type="password" id="passwordField">
        <input type="button" value="Login" onclick="checkPassword()">
        <p><output id="outputField"></output></p>
    </form>
</body>

</html>

here is the script.

var passwordField = "apple"
var checkPassword = function (checkPassword, passwordField) {
if (passwordField == "apple") {
    {document.loginForm.outputField.value = "You did it!";}
}
else {
    {document.loginForm.outputField.value = "Fail.";}
}
};
2
  • 1
    You're very confused. You don't want a checkPassword parameter to your function--that's its name. Also, you're calling checkPassword on the click, but not passing it any parameter. Why would you expect it to work? By the way, if-else is not a function, it's a statement. Commented Dec 8, 2014 at 5:21
  • I am in fact very confused, sorry I'm very new to coding. This clears a lot up. Commented Dec 8, 2014 at 5:49

2 Answers 2

1

My recommendation

var passwordField = 'apple';

function checkPassword(input) {
    return (input == 'apple') ? 'You did it!' : 'Fail';
};

call it like so

<script>
    document.write(checkPassword(passwordField));
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Why would either of those things make a difference? And what exactly is wrong with var checkPassword?
0

First, in JavaScript:

Remove checkPassword parameter from method checkpassword. Why do you need it? You don't need inner brackets, it makes no sense.

var passwordField = "apple";
var checkPassword = function (passwordField) {
    if (passwordField == "apple") {
        document.loginForm.outputField.value = "You did it!";
    }
    else {
        document.loginForm.outputField.value = "Fail.";
    }
};

And in HTML:

You have to pass password to your function, otherwise it will be undefined and it will never pass.

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="script.js"></script>
    <title>Login</title>
</head>

<body>
    <form name="loginForm">
        <label>Password:</label> 
        <input type="password" id="passwordField">
        <input type="button" value="Login" onclick="checkPassword(document.getElementById('passwordField').value)">
        <p><output id="outputField"></output></p>
    </form>
</body>

</html>

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.