-1

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>
4
  • Why are you calling the function of page load , I think you want to call the access function on some event Commented Dec 5, 2015 at 20:30
  • 1
    You will want to name your form and use document.forms[formname].userid.value, (and .psw.value), and you'll also want to keep the username and password secret - use a server side script (Bash, Perl, PHP, Python, JavaScript) or program (C, Java, C#,...). Commented Dec 5, 2015 at 20:34
  • @Kenney How do you use Java in HTML/JS? Commented Dec 5, 2015 at 20:35
  • Please edit your question to include what your code currently does, what errors it produces if any, and what the desired behavior is. For more information please read How To Ask. Thanks! Commented Dec 5, 2015 at 20:43

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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/

1 Comment

Thanks all. I understand the issues now. Very basic errors from me, although I am fairly weak at coding myself.

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.