Reputation: 3186
OK, I have a list of <li>
s with some content in like so:
<div>
<ul>
<li><h4>...</h4><div>...</div></li>
<li><h4>...</h4><div>...</div></li>
<li><h4>...</h4><div>...</div></li>
<li><h4>...</h4><div>...</div></li>
<li><h4>...</h4><div>...</div></li>
<li><h4>...</h4><div>...</div></li>
<li><h4>...</h4><div>...</div></li>
</ul>
</div>
Using jQuery, the <div>
is hidden. When one of the <h4>
s is 'clicked' the <div>
is made visible, and when clicked again is made invisible.
This all works fine except, when clicking on the <h4>
in any <li>
all of the <div>
s are made visible.
How do I stop this happening? I only want the <div>
in the same <li>
that the <h4>
that is clicked to be made visible.
This is my jQuery:
$(document).ready(function() {
$('div div').hide();
$('div h4').click(function(){
if($('div div').is(':hidden')) {
$('div div').show();
$('div li').addClass('open');
}
else {
$('div div').hide();
$('div li').removeClass('open');
}
});
});
Upvotes: 0
Views: 2835
Reputation: 1
I'd suggest .siblings().first() to just affect the first sibling jQuery finds.
Upvotes: 0
Reputation: 917
you can also use the 'siblings' selector
$(document).ready(function() {
$('div div').hide();
$('div h4').click(function(){
$(this.parentNode).toggleClass('open');
$(this).siblings('div').toggle();
});
});
Upvotes: 4
Reputation: 89322
The following should work as your click event for $("div h4").click(...).
$(this.parentNode).toggleClass("open").children("div").toggle();
Upvotes: 0
Reputation: 7695
By selecting $('div div')
you are selecting all divs that are children of other divs. This works for your initial hide, since you are hiding all the divs, but in the if statement you probably want something more like $(this).next()
Upvotes: 0
Reputation: 546
Try this:
$(document).ready(function() {
$('div div').hide();
$('div h4').click(function(){
var $li=$(this).closest('li');
var $innerDiv=$li.find('div')
if($innerDiv.is(':hidden')) {
$innerDiv.show();
$li.addClass('open');
}
else {
$innerDiv.hide();
$li.removeClass('open');
}
});
});
Upvotes: 1
Reputation: 37777
The click handler function is using the normal $('foo')
which is looking through the global document. I'd bet the solution involves some use of this
, referring to the actual specific h4
element that has been clicked on, not to "everything in the document".
Upvotes: 0
Reputation: 3482
$(document).ready(function() {
$('div div').hide();
$('div h4').click(function(){
if($(this).parents('div').is(':hidden')) {
$(this).find('div').show();
$(this).addClass('open');
}
else {
$(this).parents('div').hide();
$(this).removeClass('open');
}
});
});
Upvotes: 1