jQuery length property

jQueryWeb DevelopmentFront End Technology

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 −

raja
Published on 31-Dec-2019 13:55:16
Advertisements