Reputation: 1
I have this jquery code which slidetoggles the content on click. This code works, I just want to know if there is a simpler way to do it. I have tried slice() with no luck. I'm trying to figure out a way to do this without having to write code for each item I want to toggle.
Thanks,
Kevin
<script type="text/javascript">
$("div.accord_panel").hide();
$(document).ready(function () {
$("p.accord_header:eq(0)").click(function () {
$("div.accord_panel:eq(0)").slideToggle("slow");
$("div.accord_panel:eq(1)").hide();
$("p.accord_header:eq(1)").show();
});
$("p.accord_header:eq(1)").click(function () {
$("div.accord_panel:eq(1)").slideToggle("slow");
$("p.accord_header:eq(0)").show();
$("div.accord_panel:eq(0)").hide();
});
});
</script>
And the CSS:
p.accord_header
{
margin: 0;
padding: 0;
font-family: 'Helvetica Neue' , Helvetica, Arial, Sans-Serif;
color: #615E5A;
font-size: 9pt;
font-weight: bold;
background-color: #f3f0ed
}
div.accord_panel
{
margin: 0;
padding: 0;
font-family: 'Helvetica Neue' , Helvetica, Arial, Sans-Serif;
color: #615E5A;
font-size: 9pt;
display: none;
background-color: #f3f0ed
}
and HTML:
<p style="text-align: -webkit-auto;" class="accord_header"><strong><span>+ El Salvador/San Diego de Tenango Task Force</span><br />
<br />
</strong></p>
<strong>
</strong>
<div class="accord_panel"><strong><span>In partnership with Agros, Int'l since 2001, UPC has come alongside this rural village to encourage them as they move toward economic self-sufficiency. Most importantly we send service teams in January and July to renew friendships, share the love and gospel of Jesus Christ and participate in village activities.<br />
<br />
<br />
Julie Thomas // 425.881.6185</span><br />
</strong>
</div>
<p style="text-align: -webkit-auto;" class="accord_header"><strong><span>+ El Salvador/San Diego de Tenango Task Force</span><br />
<br />
</strong></p>
<strong>
</strong>
<div class="accord_panel"><strong><span>In partnership with Agros, Int'l since 2001, UPC has come alongside this rural village to encourage them as they move toward economic self-sufficiency. Most importantly we send service teams in January and July to renew friendships, share the love and gospel of Jesus Christ and participate in village activities.<br />
<br />
<br />
Julie Thomas // 425.881.6185</span><br />
Upvotes: 0
Views: 1137
Reputation: 50177
You can just pass in an event argument to the click function to be able to use the event.target
property to get the clicked element, then get the next.accord_panel
sibling of that element to slide down after sliding up all the panel elements:
$("p.accord_header").click(function(e) {
$("div.accord_panel").stop(true, false).slideUp();
$(e.target).closest('p').next('.accord_panel').stop(true, false).slideDown();
});
This will work with any number of headers and panels.
Upvotes: 1
Reputation: 1372
here is your answer. Jquery Tools, easy to use and ligthweight.
Upvotes: 1