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

asked on

jQuery: Find matching children but not matching grandchildren

Using jQuery, how can I select matching children but not matching grandchildren?


<!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 () {
  $(this).find('ul').show()
 });
});

</script>

</head>
<body>
When you click on "Vegitation" only "Fruit" and "Vegies" should be shown. "Apple" and "Peas" should not be shown.
 <ul>
  <li>Vegitation
   <ul>
    <li>Fruit
     <ul>
      <li>Apple</li>
      <li>Peach</li>
      <li>Pear</li>
     </ul>
    <li>Vegies
     <ul>
      <li>Spinich</li>
      <li>Peas</li>
     </ul>
    </li>
   </ul>
  </li>
 </ul>

</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Snarf0001
Snarf0001
Flag of Canada 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
Your HTML is malformed, there's a missing </li>

When you click on "Vegitation" only "Fruit" and "Vegies" should be shown. "Apple" and "Peas" should not be shown.
<ul>
    <li>Vegitation
        <ul>
            <li>Fruit
                <ul>
                    <li>Apple</li>
                    <li>Peach</li>
                    <li>Pear</li>
                </ul>
            </li>
            <li>Vegies
                <ul>
                    <li>Spinich</li>
                    <li>Peas</li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

Open in new window