I would like to automatically turn list items into jQuery toggles for elements in a page.
For example, here is some sample page code:
<div id="page">
<div id="menu">
<ul>
<li class="books">Books</li>
<li class="cars">Cars</li>
<li class="sausages">Sausages</li>
</ul>
</div>
<div id="elements">
<div class="books">This layer displays a range of books</div>
<div class="cars">This layer displays a range of cars</div>
<div class="sausages">This layer displays a range of sausages</div>
</div>
</div>
As you can see, each menu list item and each element DIV, share the same class name.
What I would like to accomplish, with jQuery, is turning each menu list item automatically into a toggle, which can then toggles its corrosponding DIV in the elements section.
So it basically needs to read the menu items, then see if that menu item has a corrosponding element layer, and if so, turn it into a toggle which can toggle the layer.
It needs to be dynamic, because menu items and element layers might be added and removed regularly and it could eventually incorporate dozens of layers.
If this is possible, please demonstrate with a jsFiddle.
Thanks!
EDIT: I know how to use jQuery to hide and show specific elements on a page, where you hard code the element name into the code, however, I have no idea how to make it dynamic. I tried googling for at least 2 hours prior to posting here.