Solved

jQuery: Clicking on grandchild hides everything

Posted on 2014-02-16
1
312 Views
Last Modified: 2014-02-16
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

0
Comment
Question by:hankknight
1 Comment
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39862943
use parents instead of parent to exclude all ancestors of the clicked node:
$('ul').not(  $(this).parents()  ).not(  $(this).find('ul')  ).slideUp(200);

Open in new window

http://jsfiddle.net/RWdfZ/1/
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now