Search code examples
jqueryajax

sliding panel did't close on link click


i have one slide panel login box with jquery and its works fine only close link did't work here is javascript code.

 <script type="text/javascript">
$(document).ready(function(){
    $("#login-link").click(function(){
        $("#login-panel").slideToggle(200);
    })
})
$(document).keydown(function(e) {
    if (e.keyCode == 27) {
        $("#login-panel").hide(0);
    }

});

$("#login-close").click(function() {
$("#login-panel").hide(0);
return false;
});

</script>

here is HTML for

<a id="login-link" href="#" title="Login">Login</a>   
<div id="login-panel">
<div style="padding-left:10px;width:190px; height:163px; background-color:#2a2a2a; text-align:left; vertical-align:text-top;">
<form id="login" action="<?php echo osc_base_url(true) ; ?>" method="post">
<table border="0" style="font: normal 10pt Tahoma; color:#fff;">
  <tr>
    <td height="20" valign="top">Email <br>
    <input id="email" type="text" value="" name="email"></td>
  </tr>
  <tr>
    <td height="20" valign="top">Password<br>
    <input id="password" type="password" value="" name="password">
  </tr>
   <tr>
  <td><input type="submit" name="submit" value="Sign In" /></td>
  </tr>
  <tr>
    <td height="25" valign="top">
     <small><a href="#login-close">press ESC or close</a></small></td>
  </tr>
</table>
</form>
</div>

it works perfect and also close the login box when i ESC is pressed. but it did't close on link click, it only add #login-close in url

Thanks


Solution

  • You don't have any element with an id of login-close. Try change your close anchor to this:

    <a href="#login-close" id="login-close">press ESC or close</a>
    

    Also, make sure all your code is inside the document ready function.

    $(document).ready(function(){
        $("#login-link").click(function(){
            $("#login-panel").slideToggle(200);
        });
    
        $(document).keydown(function(e) {
            if (e.keyCode == 27) {
                $("#login-panel").hide(0);
            }
        });
    
        $("#login-close").click(function() {
            $("#login-panel").hide(0);
            return false;
        });
    });