title: How to search for all SPAN elements inside the DIV recursively?
body: <p>I'm trying to find all the SPAN elements that are inside the DIV, and the function returns an array of the SPAN found.</p>
  <p>Below I made a recursive function, but it brings 43 SPAN, I expected 7 SPAN:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function findRecursive(myElement, elementName, element) {
      if (element == null){
          element = {'element':null, 'array':[]}
      }
  
      if (element['element']) {
          myElement = element['element']
      }
  
      if (element['array'] === undefined) {
          element['array'] = []
      }
  
      var arr = element['array']
      var els = myElement.getElementsByTagName(elementName)
  
      for (let i = 0; i &lt; els.length; i++) {
          const e = els[i];
          if (e.tagName == elementName) {
              arr.push(e)
          }
          args = {'element':e, 'array': arr}
          findRecursive(null, elementName, args)
      }
      return arr
  }
  
  var e = document.getElementById('main')
  var result = findRecursive(e, 'SPAN')
  console.log(result)</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;title&gt;Document&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="main"&gt;
          &lt;span&gt;
              &lt;span&gt;
                  &lt;span&gt;
                      &lt;span&gt;
                          &lt;span&gt;
                              mySpan1
                          &lt;/span&gt;
                      &lt;/span&gt;
                  &lt;/span&gt;
                  &lt;span&gt;
                      &lt;span&gt;
                          mySpan2
                      &lt;/span&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
          &lt;/span&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>What am I wrong with this code? I should bring 7 SPAN</p>