JQuery - How to Count Child Elements in Div?
Hi All,
This article goes in detailed on jquery how to count elements in div. we will help you to give example of jquery count elements in div with class.
Here you will learn jquery count number of elements in div. i would like to share with you jquery number of elements in div.
Let's see bellow simple example of count child elements in div using jquery. you can also count specific tag in div using jquery. i will give two example that will help you.
Example: Count "p tag" in Div
<!DOCTYPE html>
<html>
<head>
<title>jquery count number of elements in div - itsolutionstuff.com</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<h1>jquery count number of elements in div - itsolutionstuff.com</h1>
<div id="myDiv">
<h1>Hi</h1>
<p>Hello 1</p>
<p>Hello 2</p>
</div>
<button class="btn1">Click Me!</button>
<script>
$(document).ready(function(){
$('.btn1').click(function(){
var elementCount = $('#myDiv p').length;
alert(elementCount);
});
});
</script>
</body>
</html>
Example: Count all tag in Div
<!DOCTYPE html>
<html>
<head>
<title>jquery count number of elements in div - itsolutionstuff.com</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<h1>jquery count number of elements in div - itsolutionstuff.com</h1>
<div id="myDiv">
<h1>Hi</h1>
<p>Hello 1</p>
<p>Hello 2</p>
</div>
<button class="btn1">Click Me!</button>
<script>
$(document).ready(function(){
$('.btn1').click(function(){
var elementCount = $('#myDiv *').length;
alert(elementCount);
});
});
</script>
</body>
</html>
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get Number of Characters in a String in JQuery?
- JQuery Radio Button Change Event Example
- How to Set Radio Button Checked Based on Value using JQuery?
- How to Get Checked Radio Button Value by Name in JQuery?
- How to Check If Checkbox is Checked or Not in JQuery?
- PHP Signature Pad Example | E-Signature Pad using Jquery Ajax and PHP
- How to Disable F5 Refresh Button using JQuery?
- JQuery Select Box with Search using Select2 JS Example