Reputation: 1800
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?
Upvotes: 0
Views: 239
Reputation: 56539
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
Upvotes: 4
Reputation: 57105
use proper if
if (a == 'qwe' && b == 'rty' )
Upvotes: 2