jQuery length property


The length property in jQuery is used to get the number of elements in the jQuery object.

Syntax

The syntax is as follows −

$(selector).length

Example

Let us now see an example to implement the jQuery length property 

 Live Demo

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("button").click(function(){
         document.getElementById("demo").innerHTML = "<br>Count = " + $("li").length
      });
   });
</script>
</head>
<body>
<h2>Devices</h2>
<ol>
<li>TV</li>
<li>Laptop</li>
<li>Headphone</li>
<li>Earphone</li>
<li>SSD</li>
</ol>
<button>Count of li</button>
<p id="demo"></p>
</body>
</html>

Output

This will produce the following output −

Click “Count of li” to get the number of li elements −

Updated on: 31-Dec-2019

278 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements