.length in JQuery


Description: The number of elements in the jQuery object.

  • version added: 1.0length

The number of elements currently matched. The .size() method will return the same value.


Count the divs. Click to add more.

<!DOCTYPE html>

  body { cursor:pointer; }
  div { width:50px; height:30px; margin:5px; float:left;
        background:green; }
  span { color:red; }
  <script src="/scripts/jquery-1.4.js"></script>
<script>$(document.body).click(function () {
      var n = $("div").length;
      $("span").text("There are " + n + " divs." +
                     "Click to add more.");
    }).trigger('click'); // trigger the click to start</script>

Leave a Reply

Your email address will not be published. Required fields are marked *