Saturday, May 25, 2024
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  194] [ 2]  / answers: 1 / hits: 21641  / 13 Years ago, thu, december 22, 2011, 12:00:00

In jQuery or JS I need to count the amount of DIV elements inside my parent DIV called cont? I've seen similar questions here on StackOverflow and have tried the following.



<div class=b-load id=cont>
<div>
<img src=page2.jpg alt=/>
</div>
<div>
<img src=page3.jpg alt= />
</div>
<div>
<img src=page4.jpg alt= />
</div>
<div>
<img src=page5.jpg alt= />
</div>
</div>

function countPages() {
var maindiv = document.getElementById('cont');
var count = maindiv.getElementsByTagName('div').length;
alert(count);
}


The child DIV's are dynamically produced, so I need to count them after the page has finished loading. The problem I have is the function I wrote counts 13 DIV's and in this example, there should only 4!! Any help gratefully received..


More From » jquery

 Answers
11
console.log($(#cont div).length);

[#88430] Tuesday, December 20, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elliem

Total Points: 415
Total Questions: 117
Total Answers: 94

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
;