Question

<a class="btn">Click ME </a>

<div class="content">
    Content
</div>

$('.btn').click(function(){
    $('.content').slideToggle();
});

fiddle here

Working fine , but my question is Need slide Up div.content when we click outside .btn button

Thanks

Was it helpful?

Solution 2

Try this , More usable and simple code, I think

$('.btn').click(function(){
    $('.content').delay(100).slideDown( function(){
});

});
$('html').click(function(){
  if( $('.content').is(':visible') ) {
     $('.content').slideUp();
  }
});

Demo

OTHER TIPS

$('.btn').click(function(e){
     $('.content').slideToggle();
     e.stopPropagation();
});

$(document).click(function(){
     $('.content').slideUp();
})

Working Fiddle

Use below code:

$("body").click
(
  function(e)
  {   
    if(e.target.className !== "btn")
    {
      $('.content').slideToggle();
    }
  }
);

http://jsfiddle.net/zFUUq/5/

Try

$('.btn').click(function(){
    $('.content').slideToggle();
    return false;
});

$(document).click(function(e){
    if(!$(e.target).closest('.content').length){
        $('.content').slideUp();
    }
})

Demo: Fiddle

What this does is looks for all clicks.

If the click is not on content, and not on btn, it will slide up the menu.

 $(document).click(function(e){
    if (!$(e.target).hasClass('content') &&  !$(e.target).hasClass('btn') ) {
         $('.content').slideUp();
    }
});

Working DEMO

Try this

$('.btn').click(function (event) {
    $('.content').slideToggle();
    event.stopPropagation();
});
$(document).click(function() {
   $('.content').slideUp();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top