-6

I want login area (in code below) to toggle on login button click. Also, I need login area hidden when clicked anywhere outside of login area, but not on login button.

<button id="loginBtn">Login</button>

<div id="loginArea">
   <!-- here is login form -->
</div>

How can I achieve this?

eomeroff
  • 9,599
  • 30
  • 97
  • 138
  • 7
    @eomeroff: You have 1151 rep - surely you should know by now that people will ask you - 'what have you tried so far'... – Paul Mar 12 '13 at 13:29
  • how about using selector :not ? – egig Mar 12 '13 at 13:30
  • @Westie I do not have 1151 any more. And I do not see any one asking what have I tried. What if I do not have a clue how to try. This is specific question that could not research. Honestly I do not see the reason why 5 down votes. Hope the question will be useful for someone. – eomeroff Mar 12 '13 at 13:42
  • @Ravi I can not use UI modal dialog box. Thanks. – eomeroff Mar 12 '13 at 13:45

3 Answers3

2

Using:

$('body').on('click', '#loginBtn', function(){
    $('#loginArea').show();
});

Will show the login, while the following will remove it when you click outside of it:

$(document).on('click', 'body:not(#loginArea)', function(){
    $('#loginArea').hide();
});
Tom Walters
  • 15,366
  • 7
  • 57
  • 74
1
$(function() {
    $('#loginBtn, #loginArea').on('click', function(e) {
        $('#loginArea').show();
        e.stopPropagation();
    });
    $('body').on('click', function() {
        $('#loginArea').hide();
    });
});

If the click is within the login area or on the button, then the event will not make it to the second handler to hide the login area.

Matt Cain
  • 5,638
  • 3
  • 36
  • 45
-1

How's that?

$('#loginBtn').click(function (e) {
   $('#loginArea').toggle();
   e.stopPropagation();
});

$(document).on('click',function () {
    $('#loginArea').hide();
});
Dogoku
  • 4,585
  • 3
  • 24
  • 34