-1

I just started learning javascript and I have been on HTML for some time now. I know I have successfully coded a right JavaScript code for my log in form.

Here is my JavaScript code:


<script>
function login(){
    var username=document.getElementById('usrnm');
    var password=document.getElementById('pass');
    if(username=="abc"){
        if(password=="123"){
            window.location.href="mypage.html"
        }
        else{
            window.location.href="error.html"
        }
    else{
        window.location.href="error.html"
    }
}
</script>

And here is my HTML form:


<form name="input" method="get">
<b>Username:</b>
<br>
<input id="usrnm" type="text" name="usrnm">
<br>
<br>
<b>Password:</b>
<br>
<input id="pass" type="password" name="pswrd">
<br>
<br>
<center>
<input id="login" type="image" src="login.png" value="login" onclick="login()">

When I click on the Log in button. It only reloads the same page it is in but as you can see, I intended to call "mypage.html". I'mm just starting to learn JavaScript so if there are any obvious reasons why my code wont work, be easy. Thanks =). I know my JavaScript works because when I tried inserting onclick="login()" to the line <form name="input" method="get"> the code directs me to "error.html" when I type on the Username field or Password field.

4
  • 3
    Add .value to username and password. Like this if(username.value == "abc") Commented Aug 8, 2013 at 12:06
  • You have a typo in your if condition, you are missing } of main if Commented Aug 8, 2013 at 12:06
  • 1
    you can shorten code if(username=="abc" && password=="123" ){ success page } else { go error page } Commented Aug 8, 2013 at 12:07
  • refer this question stackoverflow.com/questions/11810874/… Commented Aug 8, 2013 at 12:24

4 Answers 4

4

You are not getting the element values, just the elements. Add .value to the end of your document.getElementById(id) method calls.

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

Comments

4

You JS does not work as intended. You are selecting elements, but not their values. Try changing to the following:

<script>
function login(){
    var username=document.getElementById('usrnm').value;
    var password=document.getElementById('pass').value;
    if(username=="abc"){
        if(password=="123"){
            window.location.href="mypage.html"
        }
        else{
            window.location.href="error.html"
        }
    }
    else{
        window.location.href="error.html"
    }
}
</script>

6 Comments

that is exactly my answer
@DKM,@imulsion we posted almost simultaneously, sorry. Copy-pasted OP's code, didn't notice, now edited.
it Happens :) no worries ;)
@ArtyomNeustroev Neustroev, Still does not work. If you want I can post my disorganized, 657 line code. Cx
@CarloSolomonFernandez try to debug your login function to see what exaclty happens.
|
0

Get the usrnm id, pass id values as

var username=document.getElementById('usrnm').value;
var password=document.getElementById('pass').value;

OR

Otherwise pass if condition as

if(username.value == "abc"){
    if(password.value == "123"){
      .....
    }
}

Comments

0

change the code

var username=document.getElementById('usrnm').value;

var password=document.getElementById('pass').value;

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.