jQuery inside body tag only?

245 Views Asked by At

Is there a way to count children only inside element using jQueru or just javascript?

I tried several methods like:

var count = $(document.body).children().length

or

var count = 0;
$(document.body).children().each(function(){
   count +=1
});

And every single one counts elements even outside of tag even and scripts or styles!

So Is there a way to count elements only inside body tag?

1

There are 1 best solutions below

4
On

To get the number of elements only inside the body tag, try

var btags = document.body.getElementsByTagName("*");
var count = btags.length;