JavaScript: recursive function does not work like it should
<p>I'm trying to do a recursive function to find HTMLelement by class, but for some reason it stops after it reaches a child element without children. Here is the code:</p>

<hr>

<pre><code>var alpha = Obj.elementByClass(document.body, "message");

Obj.elementByClass = function(element, cl) {
   var elementChildren = element.children,
       elementChildrenLength = elementChildren.length;

   for(var num=0; num&lt;elementChildrenLength; num++) {
      if(elementChildren[num].className &amp;&amp; elementChildren[num].className.indexOf("cl") &gt; -1) {
         return elementChildren[num];
      }
      else if(elementChildren[num].children.length !=0) {
         return Obj.elementByClass(elementChildren[num], cl);
      }
   }
};
</code></pre>

<hr>

<p>Please don't advice jquery or other libraries, I would like to understand why it stops when it reaches the element without children.</p>

<p>Thanks!</p>
