Huzaifah Farooq
Huzaifah Farooq

Reputation: 52

HTML and Javascript. Why does my code not work

This is a quite simple piece of HTML with a bit of JavaScript integrated. I did what I could from what I know, but I don't think I used the correct document tags. Its either that or the page needs a way of updating, which I don't know how to do. Please help.

<!DOCTYPE html>
<html>
<body>
    <form action="">
        User name:<br>
        <input type="text" name="userid">
        <br>
        User password:<br>
        <input type="password" name="psw">
    </form>

    <p>The characters in a password field are masked (shown as asterisks or circles).</p>

    <p id="enter"></p>

    <script>
        function access(username, password) {
            if (username == "bobby") {
                if (password == "qwertyasd123") {
                    document.getElementById(enter).innerHTML = "You may pass";
                }
            }
        }
        access(document.getElementsByName("userid").innerHTML, 
            document.getElementsByName("userid").innerHTML)
    </script>
</body>
</html>

Upvotes: -1

Views: 76

Answers (2)

CoderPi
CoderPi

Reputation: 13221

there where quite some mistakes indeed:

of document.getElementsByName("userid") you get an list, so you want to access the first by [0]:

document.getElementsByName("userid")

You forgot to use the id as a string here: document.getElementById(enter) should be document.getElementById("enter")

You used "userid" twice and not "psw" aswell:

access(document.getElementsByName("userid")[0].value, 
            document.getElementsByName("psw")[0].value)

You want it to happen when you press enter:

<form onsubmit='access(document.getElementsByName("userid")[0].value, document.getElementsByName("psw")[0].value)'>

and you will need a <input type="submit" name="sub"> for that aswell.

Here is the complete code and the working example: https://jsfiddle.net/j35fbet9/

Upvotes: 0

Mex
Mex

Reputation: 999

Change

document.getElementById(enter)

to

document.getElementById('enter')

Also change:

document.getElementsByName("userid").innerHTML

to

document.getElementsByName("userid")[0].value

And you didn't change userid to psw for one your strings.

Upvotes: 2

Related Questions