I’m trying to use show/hide on a submenu. It looks like this:
- Parent 1
- Parent 2
- Child A
- Child B
- Parent 3
- Child C
- Child D
I only want to show the submenu when I click on its parent. Currently, whenever I click on any parent, I get all of the submenus.
Like so: http://jsfiddle.net/saltcod/z7Zgw/
Also, clicking on a link in the submenu toggles the menu back up.
Demo: http://jsfiddle.net/jasper/z7Zgw/1/
Basically the code above uses
thisto reference the clicked<li>element so we can find the.childelement that is a child of the clicked<li>element.This:
$('.child')Changed to:
$(this).children('.child')Update
Also you can stop the propagation of the
clickevent on the nested.childelements like this:Demo: http://jsfiddle.net/jasper/z7Zgw/9/
Docs:
event.stopPropagation(): http://api.jquery.com/event.stopPropagation.children(): http://api.jquery.com/children