Knowledge Walls
Gopal Rao
Mumbai, Maharashtra, India
Passcode:
How to count number of divs in jquery in Jquery functions of JQuery Examples
3649 Views
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 
Best Lessons of "JQuery Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details