Count child elements in jQuery

Description

The following code shows how to count child elements.

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() {<!--  w w w.  ja v a2 s. com-->
    $("#container").click(function(e) {
      var $ch = $(e.target).children();
      alert($ch.length);
      e.preventDefault();
      return false;
    });
  });
</script>
</head>
<body>
<body>
  <div id="container">
    <div>
      <p>
        This <span>is the <em>way</em> we
        </span> write <em>the</em> demo, java 2s.com
      </p>
    </div>
  </div>
</body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Example »




Animation
DOM
Event
Form
List
Select
Style
Table
Utilities