ItSolutionStuff.com

JQuery - How to Count Child Elements in Div?

By Hardik Savani • July 21, 2023
jQuery

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...

Tags: jQuery
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Get Number of Characters in a String in JQuery?

Read Now →

JQuery Radio Button Change Event Example

Read Now →

How to Set Radio Button Checked Based on Value using JQuery?

Read Now →

How to Get Checked Radio Button Value by Name in JQuery?

Read Now →

How to Check If Checkbox is Checked or Not in JQuery?

Read Now →

PHP Signature Pad Example | E-Signature Pad using Jquery Ajax and PHP

Read Now →

How to Disable F5 Refresh Button using JQuery?

Read Now →

JQuery Select Box with Search using Select2 JS Example

Read Now →