jQuery Selector How to - Access grand child elements from the DOM








Question

We would like to know how to access grand child elements from the DOM.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!-- w ww .ja  v a  2 s  . c  om-->
    console.log($('ul:eq(1) > li').text())
});
</script>
</head>
<body>
  <ul>
    <li>Sample</li>
    <li>Sample</li>
    <li>Sample</li>
    <li>
      <ul>
        <li>Need to Get access to this li</li>
      </ul>
    </li>
    <li>Sample</li>
  </ul>
</body>
</html>

The code above is rendered as follows: