0

In my website I have a box that is not displayed. When the user types his username and password, if they are correct, the box must be displayed. Here you see the HTML code:

<div id="mainbox" style="display: none;">
    <p class="classm">Test.</p>
</div>    
<input type="text" id="user"/>
<br />
<input type="text" id="password" value="a"/>
<br />
<input type="button" value="Log in" onclick="login();">

And here the javascript function:

function login() {
    var a = document.getElementById('password').value;
    var b = document.getElementById('user').value;
    if ((a == 'qwe') && (b == 'rty')) { //the problem is not here
     document.getElementById('password').style.display="none";
    }
    else
    {
     alert('You are not logged in.');  
    }
}

When I click the button Log in looks like the function login(); is not called because anything happen. Do you know why?

2
  • 1
    Are you sure you are typing the right letters in the right box? The first box needs to be 'rty' and the second 'qwe'. Commented Aug 31, 2013 at 10:56
  • working fine: jsfiddle.net/5dgtz Commented Aug 31, 2013 at 10:56

2 Answers 2

4
if (a == 'qwe') && (b == 'rty') //Wrong

Enclose the condition like if( condition )

 if ((a == 'qwe') && (b == 'rty') ) //Corect

As @Frédéric Hamidi suggested, there is no need of inner parentheses.

Simple like this

if (a == 'qwe' && b == 'rty' )

Demo here

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

3 Comments

Nitpicking: == has more precedence than &&, so the inner parentheses are not required.
@AlbertoRossi, I think you have a & b mixed, a should be user, and b pass, or? I added a demo to this answer. Check it out.
@FrédéricHamidi You're right, I have included in it in my answer. Thanks for the pointers.
2

use proper if

if (a == 'qwe' && b == 'rty' )

fiddle

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.