I need to count the child elements in a <div>
with jQuery:
<div id="home">
<div>..</div>
<div></div>
</div>
The jQuery code I tried is:
console.log($('#home').length);
But it prints 0.
Anyone can help me?
I need to count the child elements in a <div>
with jQuery:
<div id="home">
<div>..</div>
<div></div>
</div>
The jQuery code I tried is:
console.log($('#home').length);
But it prints 0.
Anyone can help me?
$( document ).ready(function() {
var divLength = $('#home').children().length;
console.log(divLength);
});
Are you trying to count the div
's inside #home
?
If so, try this:
$(document).ready(function() {
console.log($('#home div').length);
});
Try wrapping your code in a document ready function,
$( document ).ready(function() {
console.log($('#home').length);
});
Use *
to count all elements in #home
like following.
$('#home *').length
If you want to count only direct child the use direct child selector >
.
$('#home > *').length
After completion of loading of DOM, try this code.
$('#home').children().length;