0

i have a list in my HTML page with the following code.

        <li><a href="HomePage.html">Home</a></li>
        <li><a href="#" >Register</a></li>
        <li><a href="#" onclick="loginCheck();">Contact Us</a></li>
        <li><a href="MakePayment.aspx" onclick="loginCheck();">Services</a></li>
        <li><a href="HomePage.html" onclick="logout();">Log Out</a></li>

my log in check function is as follows.

 function validate()
   {
   some validations done here.if found true change the variable log in to true.
   login = true;
   }
 function loginCheck() {

       if (!login) {
           alert("Log In to continue operations");
           return false;
       }

the page gives an alert Log In to continue operations. But still moves to the next page which was clicked. How can i stop this ?

2
  • 2
    Your event handler is not actually returning false. You have to include the return statement in the "onclick" attribute value. Commented Aug 24, 2014 at 13:01
  • You can also prevent the default value. Commented Aug 24, 2014 at 13:03

1 Answer 1

3

You need to include the return in the onclick handler, otherwise the value isn't actually returned.
Do it like this and it will work:

<li><a href="HomePage.html">Home</a></li>
<li><a href="#" >Register</a></li>
<li><a href="#" onclick="return loginCheck();">Contact Us</a></li>
<li><a href="MakePayment.aspx" onclick="return loginCheck();">Services</a></li>
<li><a href="HomePage.html" onclick="return logout();">Log Out</a></li>
Sign up to request clarification or add additional context in comments.

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.