Get the total number of matched elements in jquery

In this programming tutorial we will learn how to get the total number of matched elements in jquery. As we all know, jquery is a very powerful javascript library and its selectors play very vital role to select any DOM element. In some cases we need to know the total number of matched elements, for this, let's have a look over the following code snippet.
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
	<div>Welcome to nice-tutorials.blogspot.com</div><br/>
	<div>Welcome to nice-tutorials.blogspot.com</div><br/>
	<span></span>

<script language="javascript" type="text/javascript">
$("span").html("There are <strong>" + $("div").size() + "</strong> divs in this page.");

</script>

</body>
</html>

Output:-
Welcome to nice-tutorials.blogspot.com
Welcome to nice-tutorials.blogspot.com
There are 2 divs in this page.

Note:-
The .size() method is functionally equivalent to the .length property; however, the .length property is recommended because it does not have the overhead of a function call.

So that's it. This is the code to get the total number of matched elements in jquery.

I love your feedback.

0 comments: