Firefox Or JavaScript, count the DOM

I’m sure this is simple but I have no idea how to do it.
How do i count the amount of DOM elements in my HTML page? I wanted to do this in a userscript or bookmarklet but i have no idea how to start!

Use this for Element nodes:


For any node, you can extend Node like this:

Node.prototype.countChildNodes = function() {
  return this.hasChildNodes()
    ? Array.prototype.slice.apply(this.childNodes).map(function(el) {
        return 1 + el.countChildNodes();
      }).reduce(function(previousValue, currentValue, index, array){
        return previousValue + currentValue;
    : 0;

Then all you need to do is to call document.countChildNodes.

// You could use the same method to get the count of each tag, if it matters

  function tagcensus(pa){
    pa= pa || document;
    var O= {},
    A= [], tag, D= pa.getElementsByTagName('*');
    D= A.slice.apply(D, [0, D.length]);
        tag= D.shift().tagName.toLowerCase();
        if(!O[tag]) O[tag]= 0;
        O[tag]+= 1;
    for(var p in O){
        A[A.length]= p+': '+O[p];
    A.sort(function(a, b){
        a= a.split(':')[1]*1;
        b= b.split(':')[1]*1;
        return b-a;
    return A.join(', ');


In JavaScript you can do


In jQuery you can do


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   Remove Event Listener On Unmount React

Similar Posts