Length Property and size() Method in JQuery
length and size methods are returns how many elements are selected by the JQuery selector.
Example
$(element).size()
$(element).length
Hints
size() is a method to read the length property inside of the method. To use length to avoid one method call and get faster response.
|
Example.1 How to use length property to count number of elements in JQuery?
<html>
<head>
<title>HTML-JQuery</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(function(){
var n=$(".divControl").length;
alert(n);
});
</script>
</head>
<body>
<div class="divControl">one</div>
<div class="divControl">two</div>
<div class="divControl">three</div>
<div class="divControl">four</div>
</body>
</html>
|
Example.1 Demo
|
Example.2 How to count number of element using Size() method of JQuery
<html>
<head>
<title>HTML-JQuery</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(function(){
var n=$(".divControl").size();
alert(n);
});
</script>
</head>
<body>
<div class="divControl">one</div>
<div class="divControl">two</div>
<div class="divControl">three</div>
<div class="divControl">four</div>
</body>
</html>
|
Example.2 Demo
|