0

I need a jquery help in click to to hide issue .

What I have now :

I have a text I can click and it will open div . And when I click any place this div hides .also when I click the Gray div still it's hide

*What I am trying *

I am trying to limit the hiding . IF I click on the Div it should not hide . beside that clicking other places It will hide .

Live js Fiddle demo: http://jsfiddle.net/S4hcf/

HTML

<p class="show_hide">Clcik Me</p>

<div class="slidingDiv"></div>

CSS

div{
width:200px;
height:150px;
background:#ccc;

}

p{
 cursor:pointer;

 }

jQuery

 // click to hide    
$('html').click(function(event){
    if($('.slidingDiv').is(':visible')){
       $('.slidingDiv').hide(); 
    } 
});

//Toggle

$(".slidingDiv").hide();    

$('.show_hide').click(function(event){
    event.stopPropagation();
$(".slidingDiv").slideToggle();
});

Any help will be appreciated . Thanks

S Raihan
  • 377
  • 2
  • 9
  • 18

4 Answers4

4

You can simply prevent the click bubbling up to the outer click when clicking on the div itself:

$(".slidingDiv").click(function(e){
    e.stopPropagation();
});

http://jsfiddle.net/S4hcf/4/

James Montagne
  • 77,516
  • 14
  • 110
  • 130
1

you can try

$(".slidingDiv").click(function(e){
    e.stopPropagation();

});

http://jsfiddle.net/S4hcf/1/

Abraham Uribe
  • 3,118
  • 7
  • 26
  • 34
1

Add $(event.target).hasClass('.slidingDiv') to your condition

$('html').click(function(event){
    if($(event.target).hasClass('.slidingDiv') && $('.slidingDiv').is(':visible')){
        $('.slidingDiv').hide(); 
    } 
});

DEMO: http://jsfiddle.net/S4hcf/5/

Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
1

You can initially set the showing div to display:none

      .slidingDiv{
            display:none;
                 }

and write the below script for it to toggle

  $('.show_hide').click(function(event){
    event.stopPropagation();    
    $(".slidingDiv").slideToggle();
   });

refer this fiddle http://jsfiddle.net/Kritika/qhXcf/

Pbk1303
  • 3,702
  • 2
  • 32
  • 47