Get the name and length of an element tag using jquery each

I try to get information about all element tags and there is frequency on the page, this is how I find the tag, it’s very simple:

$('body *').each(function(){
var string = this.tagName;

But the length does not catch up:

var count =  $(this).length;

It gives me 1while there are two divs , for example: JsFiddle

I can solve this problem by deleting the function each, but I need this for the tag. I have to use body * for my project, so I cannot access the div directly.

+4
source share
2 answers

Try:

var count =  $(string, 'body').length;
+7
source

body *, . , this DOM.

, , :

var tags = ['div', 'li', 'a'];

tags.forEach(function (tag) {
  console.log(tag, "appears", $('body ' + tag).length, 'times');
});

JS :

div appears 141 times
li appears 66 times
a appears 179 times 
+4

All Articles