I created a css class for 8 divs, I need to use the each function of jquery to count the divs and show the result in the page’s html, example: “8 divs appear”.
I created a code but it doesn’t work, it doesn’t show the result.
code:
<script>
$( ".class" ).each(function( index ) {
console.log( index + ": " + $( this ).text() );
});
</script>
3
Answers
After a few attempts, I managed to solve the problem.
I created three codes and they worked and gave me the result I wanted. Thanks a lot for the help.
1.
2.
3.
if you just want to count them, you can do:
$('.class').length
From
each()
method to get whole same class divsindex
+content
or You want to count how many divs has same class then simply use.length
.Note: Check on
Full page.
then you can see same class divscount result
.