Michael Michael - 5 months ago 14
jQuery Question

Make a jQuery element slide up if clicked anywhere outside of it

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

Answer

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