Reputation: 4987
There are multiple items in a div. Each item has a button and info div. When clicking on title in any div, I want to slide up or fade out everything, want to display only the info of that div in kind of lightbox style.
Here's what I'm trying and it does not work. It slides up everything on click, but does not show the info.
HTML:
<div class="wrap">
<div class="inner">
<div class="box">
<div class="button"></div>
<div class="info">Info for box 1</div>
</div>
<div class="box">
<div class="button"></div>
<div class="info">Info for box 2</div>
</div>
</div>
</div>
CSS:
.wrap{
width: 500px;
background: yellow;
padding: 20px;
}
.box{
margin-bottom: 20px;
}
.info{
display: none;
}
.button{
width: 20px;
height: 20px;
background: green;
}
jQuery:
$('.wrap').on('click','.button',function(){
$('.inner').slideUp(400);
$(this).closest('.info').slideDown(400);
});
Demo: http://jsfiddle.net/a7wwz/
Upvotes: 0
Views: 232
Reputation: 2560
How about this:
$('.wrap').on('click', '.button', function () {
$('.inner *').not($(this).parent()).slideUp(400);
$(this).next().slideDown(400);
});
Upvotes: 0
Reputation: 28196
Or try this
$('.wrap').on('click','.button',function(){
$('.inner>div>div').slideUp(400);
$(this).next('.info').slideDown(400);
});
Your original version did not work because you use .slideup()
on a container whose element you want to have visible afterwards. In my version I simply slideup only the div
s of the lowest level inside the container .inner
.
And, of course, closest()
looks upwards for the closest parent. What you really meant was next()
.
Upvotes: 1
Reputation: 388316
Try
$('.wrap').on('click','.button',function(){
var $info = $(this).next('.info').stop(true, true).slideDown(400);
$('.info').not($info).stop(true, true).slideUp();
});
Demo: Fiddle
Upvotes: 0