Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

jQuery: Clicking on grandchild hides everything

In the example below, I would like for root and current and child objects to be displayed when an item is clicked on.

The only problem is that this only works two levels deep.  For example, if I click on "Fruit" everything is hidden.  If you test this example you will see the problem.

Thanks.

http://jsfiddle.net/RWdfZ/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Examples</title>
<style type="text/css">
li {
cursor: pointer;
}

ul ul {
display: none;
}

</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {
 $('li').click(function (e) {
        e.stopPropagation();
        $('ul').not(  $(this).parent()  ).not(  $(this).find('ul')  ).slideUp(200);
        $(this).children('ul').first().slideDown('200');
 });
});

</script>

</head>
<body>

<ul>
    <li>Animals
     <ul>
       <li>Mammals</li>
       <li>Reptiles</li>
       <li>Fish</li>
     </ul>
    </li>
    <li>Minerals
     <ul>
       <li>Salt</li>
     </ul>
    </li>
    <li>Vegitation
        <ul>
            <li>Fruit
                <ul>
                    <li>Apple</li>
                    <li>Peach</li>
                    <li>Pear</li>
                </ul>
            </li>
            <li>Vegies
                <ul>
                    <li>Leafy
                         <ul>
                             <li>Kale</li>
                             <li>Spinich</li>
                             <li>Lettuce</li>
                        </ul>
                    </li>
                    <li>Roots
                         <ul>
                             <li>Kale</li>
                             <li>Spinich</li>
                             <li>Lettuce</li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>


</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial