I’m trying to create a jQuery drop-down script that will be efficient and minimal.
I need a way to associate a couple of uls with a li and reference this in an each loop.
Think of each of the clickable li titles as a cluster and the ‘child’ ul’s under them are part of this too.
I want to write a piece of efficient code, using an each loop to iterate through each of the clickable lis and expand the sublist uls associated with it.
The problem is I need to know how to associate these sublist uls with the parent li in a way that can be referenced in an each loop. No standard jquery selectors can really help me because the sublist uls are not REALLY children of the li.
HTML and Jquery attempt below. Please Help
<ul>
<li class='tier1'>This is what I want to click</li>
<ul (#1)>
<li>I want this to drop down</li>
</ul>
<ul>
<li id="gap">I want these to drop down</li>
</ul>
<li class='tier1'>Another clickable title</li>
<ul>
<li id="gap">I dont want this to drop down when I click the li at the top of this markup</li>
</ul>
</ul>
My problem is that the ul (#1) that i want to slideToggle when I click the the li above it, is not quite a child.
So I made the following code:
$(document).ready(function(){
$('.tier1').each(function(){
$(this).click(function(){
$(this).children('ul').slideToggle('slow');
});
});
});
See my example here: http://jsfiddle.net/D29mQ/2/
You have some errors in your markup. A
ulcannot be a direct descendent of aul, but can be a direct descendent of ali. I have added ahide()on theul‘s within the li.drawer, so that the content is not hidden if javascript is disabled (I would not recommend hiding these with css).and the jquery