Reputation: 13
i have a toggle function which i have working fine, only i cant get it to work for each element on the page.
my current function is
$(".tog").click(function() {
$("#shortinfo").toggle();
$('#shortinfo').toggleClass('open');
return false;
})
ive tried
$(".tog").each.click(function() {
$("#shortinfo").toggle();
$('#shortinfo').toggleClass('open');
return false;
})
and
$(".tog").each (click(function() {
$("#shortinfo").toggle();
$('#shortinfo').toggleClass('open');
return false;
I have an accordion similar too
<h1>title </h1>
<div class="shortcontent">asdasdasd</div>
<div class="longcopy">long stuff here</div>
<h1>title </h1>
<div class="shortcontent">asdasdasd</div>
<div class="longcopy">long stuff here</div>
<h1>title </h1>
<div class="shortcontent">asdasdasd</div>
<div class="longcopy">long stuff here</div>
<h1>title </h1>
<div class="shortcontent">asdasdasd</div>
<div class="longcopy">long stuff here</div>
the idea is when its clicked 'shortcontent' dissapears. at the moment that is only happening on the first section
Upvotes: 0
Views: 123
Reputation: 46183
Now that the question has changed...
You're using the wrong selector. First of all, #shortinfo
is not the same as .shortcontent
. Second, in order to make sure only the correct .shortcontent
disappears, you need to use .siblings()
.
If the .tog
element is within each accordion...
$(".tog").click(function() {
var $shortContent = $(this).siblings(".shortcontent");
$shortContent.toggle();
$shortContent.toggleClass('open');
return false;
});
If there is one .tog
element and you want all .shortcontent
to collapse...
$(".tog").click(function() {
var $shortContent = $(".shortcontent");
$shortContent.toggle();
$shortContent.toggleClass('open');
return false;
});
This should hopefully be enough to get you started.
Upvotes: 1