2

I have a drop down that I want to slideup when clicked anywhere outside of it (and all it's child elements)

My code seems to not be working :(

$(document).ready(function () {
  $('*').not(".drop").click(function() {
    $('.drop').slideUp('medium', function() {
// Animation complete.
});});});
Michael
  • 307
  • 1
  • 6
  • 17
  • 1
    possible duplicate of [jQuery hide element when clicked anywhere on the page](http://stackoverflow.com/questions/714471/jquery-hide-element-when-clicked-anywhere-on-the-page) – Felix Kling Jul 11 '11 at 17:50
  • 2
    With the due respect. You have 6 unaccepted answers, I have 100% accepted answers. Your acceptance seems to not be working. – Roko C. Buljan Jul 11 '11 at 17:51

2 Answers2

1

Try this I hope this will help you.

$(document).ready(function () {
  $('.drop').click(function(e){
     e.stopPropagation();
  });
  $('html').click(function() {
    var $drop = $('.drop');
    if($drop.is(":visible")) {
      $drop.slideUp('medium', function() {
        // Animation complete.
      });
    }
  });
 });
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • 1
    A bad solution though as it doesn't make sense to always activate slideUp() when you are not even checking whether or not it might be hidden already. – Jules Jul 11 '11 at 18:14
-1
$(document).click(function() {
    if ($('.drop').is(':visible'))
        $('.drop').slideUp();
});

$(".drop").click(function() {
    $('.drop').slideDown();
    return false;
});
Jules
  • 7,148
  • 6
  • 26
  • 50