I\'m trying to use show/hide on a submenu. It looks like this:
//select all the `- ` element that are children of the `.parent` element
$('.parent').children().click(function(){
//now find the `.child` elements that are direct children of the clicked `
- ` and toggle it into or out-of-view
$(this).children('.child').slideToggle('slow');
});
Demo: http://jsfiddle.net/jasper/z7Zgw/1/
Basically the code above uses this to reference the clicked element so we can find the .child element that is a child of the clicked element.
This: $('.child')
Changed to: $(this).children('.child')
Also you can stop the propagation of the click event on the nested .child elements like this:
$('.parent').children().click(function(){
$(this).children('.child').slideToggle('slow');
//select all the `.child` elements and stop the propagation of click events on the elements
}).children('.child').click(function (event) {
event.stopPropagation();
});
Demo: http://jsfiddle.net/jasper/z7Zgw/9/
Docs:
event.stopPropagation(): http://api.jquery.com/event.stopPropagation.children(): http://api.jquery.com/children