Categories

.length

Categories: Properties of jQuery Object Instances

.lengthReturns: Number

Description: The number of elements in the jQuery object.

  • .length

    version added: 1.0

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

  • Count the divs. Click to add more.

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