Count the parent elements by tag name in jQuery

Description

The following code shows how to count the parent elements by tag name.

Example


<html>
<head>
<script
  src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {<!--  www .j av  a 2  s  . co  m-->
    $("span").click(function() {
      var len = $("span").parents("div").length;
      alert(len);
    });
  });
</script>
</head>
<body>
  <div>
    <div>
      <span>Hello ja va2s.com</span>
    </div>
  </div>
</body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Example »




Animation
DOM
Event
Form
List
Select
Style
Table
Utilities