Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Div(message box) is not hiding at runtime using jQuery

I have a login form as following.

<li class="loginlink">
    <a id="showlogin" href="#">
        <span style="color: #666666">Login</span>
    </a>
    <div class="loginbox" style="display: block;">
        <fieldset>
            <label>User Name : </label>
            <input id="input" type="text" value="" name="input">
        </fieldset>
        <label>
            <span style="display: inline-block; ...;"> Password :</span>
        </label>
        <input id="password_txt" type="password" style="padding:5px;..;" 
               value="" name="password_txt">
        <p>
            <a class="loginlink" onclick="mojarra.jsfcljs(document.getElementById('headerForm'),
                     {'j_idt60':'j_idt60'},'');return false" style="color: #666666;.." 
                     href="#">Forgot Password?
            </a>

            <a class="loginlink" onclick="mojarra.jsfcljs(document.getElementById('headerForm'),
                      {'j_idt63':'j_idt63'},'');return false" style="..." 
                      href="#">Register 
            </a>
        </p>
        <div class="loginbuttons">
            <input id="loginBtn" type="submit" value="Login" name="loginBtn">
            <input id="cancellogin" type="button" value="Cancel">
        </div>
    </div>
</li>

When you click on showlogin. I use jQuery to display it. Like

$('#showlogin').click(function(){
    var loginBox = $('.loginbox');
    loginBox.show();
    $('.loginbox fieldset input').focus();
    if (!loginBox.is(':hidden')) {
        validateUser();              
    }         
});

$('#cancellogin').click(function(){
    $('.loginbox').hide();        
});

function validateUser() {       
    $("#loginBtn").click(function(event){

        var userName =  $("#input").val();
        var password = $("#password_txt").val();

        if (userName == "") {                
            $.dialog({                
                message: "UserName must be entered",
                imageIcon: false,
                type: "error",
                okButtonID: "ok",
                okButtonValue: "OK"                  
            });
            return false;               
        } 
        return true;             
    }); //end of click              
} //end of validateUser()

Now what is happening suppose i click on the button the box is shown like

Image 1

Now if i click on login button , then message appears

Image 2

Now if i click on Ok button. Box get disappear.

Image 3

Till here things are ok. Now suppose i close the login form by clicking on cancel button. And again open the form

Image 4

Now again click on login button. The message will appear Image 5

But now this time if i click on OK button , then the overlay gone but message don't. Why? I get something like this

enter image description here

Why this time it is not disappearing? What i am doing wrong? Please help?

Thanks

like image 562
Basit Avatar asked Aug 28 '12 10:08

Basit


1 Answers

I think the problem relates to your validateUser() function. You see every time you show the login box you bind a new click handler that contains the code to show the dialog to the login button.

function validateUser() {       
    $("#loginBtn").click(function(event){
    ...
    });
}

This means the 2nd time the login box is show the dialog is actually shown twice. The 3rd time the dialog is shown 3 times. You probably can't see this because your centering it. If you drop a Javascript alert() function after the line $.dialog.createUI = function (config) { you'll see the 2nd time the login is shown you get 2 alerts! See fiddle for example of this problem.

Unless your code does something else within the validate user function I suggest you drop this function and instead bind the click hander when the document is ready. So you end up with something like this:

$(document).ready(function () {

    $("#loginBtn").click(function (event) {

        var userName = $("#input").val();
        var password = $("#password_txt").val();

        if (userName == "") {
            $.dialog({
                message: "UserName must be entered",
                imageIcon: false,
                type: "error",
                okButtonID: "ok",
                okButtonValue: "OK"
            });
            return false;
        }
        return true;
    });

    $('#showlogin').click(function () {
        var loginBox = $('.loginbox');
        loginBox.show();
        $('.loginbox fieldset input').focus();
        // code to call validate user removed from here!
    });

    $('#cancellogin').click(function () {
        $('.loginbox').hide();
    });
});
like image 53
Chris Moutray Avatar answered Nov 20 '22 13:11

Chris Moutray