I'm trying to create a blog archive list which shows all articles by year and month (which I've done with PHP/MySQL)
Now I'm trying to make it so that on page load, all years are collapsed except the latest year/month and also that each will collapse/expand on click.
At the moment my jQuery click function will open or close all of the li elements rather than just the one I click. I'm still pretty new to jQuery so am not sure how to make it just affect the list section that I click on.
Any help would be grand!
Here's my code so far (the list is generated from PHP/MySQL loops)
<ul class="archive_year">
<li id="years">2012</li>
<ul class="archive_month">
<li id="months">September</li>
<ul class="archive_posts">
<li id="posts">Product Review</li>
<li id="posts">UK men forgotten how to act like Gentlemen</li>
<li id="posts">What Do Mormons Believe? Ex-Mormon Speaks Out</li>
<li id="posts">Here is a new post with lots of text and a long title</li>
</ul>
<li id="months">August</li>
<ul class="archive_posts">
<li id="posts">A blog post with an image!</li>
</ul>
</ul>
<li id="years">2011</li>
<ul class="archive_month">
<li id="months">July</li>
<ul class="archive_posts">
<li id="posts">New Blog!</li>
</ul>
</ul>
<li id="years">2009</li>
<ul class="archive_month">
<li id="months">January</li>
<ul class="archive_posts">
<li id="posts">Photography 101</li>
</ul>
</ul>
</ul>
And here is the jQuery so far:
$(document).ready(function() {
//$(".archive_month ul:gt(0)").hide();
$('.archive_month ul').hide();
$('.archive_year > li').click(function() {
$(this).parent().find('ul').slideToggle();
});
$('.archive_month > li').click(function() {
$(this).parent().find('ul').slideToggle();
});
});
I was experimenting with the $(".archive_month ul:gt(0)").hide(); but it didn't work as expected, it would switch the open and closed around.
Any help/thoughts?
Also, here is a fiddle for live example: http://jsfiddle.net/MrLuke/VNkM2/1/
First about the issues:
<li>
-sAnd here is how you can solve the problem - DEMO
jQuery
$('.archive_month ul').hide();
$('.months').click(function() {
$(this).find('ul').slideToggle();
});
HTML (fixed)
<ul class="archive_year">
<li class="years">2012
<ul class="archive_month">
<li class="months">September
<ul class="archive_posts">
<li class="posts">Article 1</li>
<li class="posts">Article 2</li>
<li class="posts">Article 3</li>
<li class="posts">Article 4</li>
</ul>
</li>
<li class="months">August
<ul class="archive_posts">
<li class="posts">Article 1</li>
</ul>
</li>
</ul>
</li>
<li class="years">2011</li>
<ul class="archive_month">
<li class="months">July
<ul class="archive_posts">
<li class="posts">Article 1</li>
</ul>
</li>
</ul>
</li>
<li class="years">2009</li>
<ul class="archive_month">
<li class="months">January
<ul class="archive_posts">
<li class="posts">Article 1</li>
</ul>
</li>
</ul>
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With