.length

The number of elements in the jQuery object.

.length()🡢 Integer

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

Count the divs. Click to add more.

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

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)