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

jquery, jquery-plugins

Solution

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();
    });
});

Problem

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 Now if i click on login button , then message appears Now if i click on Ok button. Box get disappear. Till here things are ok. Now suppose i close the login form by clicking on cancel button. And again open the form Now again click on login button. The message will appear But now this time if i click on OK button , then the overlay gone but message don't. Why? I get something like this Why this time it is not disappearing? What i am doing wrong? Please help? Thanks

Original source