0

i am creating an html code using text editor , but after creating the code i need to perform some actions on all sub childs (i.e tags). For that i need to know the number of levels of all the childs.

<p>
  <span style="font-size: 12pt;">
     <span style="font-family: arial, helvetica, sans-serif;">
         Sales 
     </span>
     Offer 
     <span style="background-color: #33cccc;">
        20
        <span style="background-color: #ff9900;">
           %
        </span>
     </span> 
     Off This Week
   </span>
</p> 

in short i need to count all deep levels of childs of

tag.

  • Take a look at "What if the "depth" of the data structure is unknown to me?" part of [this answer](https://stackoverflow.com/a/11922384/1169519), then try something on your own, and if you stuck, then ask a question. – Teemu Jul 08 '20 at 06:59
  • @Teemu as in my question ``` % ``` is on 3rd level so i need to know the no of deepest level – umair akhtar Jul 08 '20 at 10:50
  • As it is in your question, you've to manually count the levels. SO is not a free coding service, we expect you to ask a programming question, how to implement something is not a programming question, see https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question – Teemu Jul 08 '20 at 10:52

1 Answers1

0

You can use getElementsByTagName to get all elements. For example:

var myList = document.getElementById('myList')

var totalCount = myList.getElementsByTagName(*).length
  • This will give you a flat collection of the elements, and the count of the _levels_ is kept secret. However, this might be an answer for what OP really needs. – Teemu Jul 08 '20 at 07:04
  • This is giving no of all the childs inside that element but I need the the no of child levels. I hope you got my problem. – umair akhtar Jul 08 '20 at 10:45