Count appearances of a string in document using javascript?
<p>I want to have a javascript code that counts how many times the text "Apple" appears in the current document (let this number be N)
and creates a new div in whic it adds the aforementioned text N times.</p>

<p>So far I've managed to count how many times 'Apple' appears in the document using an example found on
the internet, like this: </p>

<h2>JS</h2>

<pre><code>window.getCount = function(parent, getChildrensChildren){
    var relevantChildren = 0;
    var children = parent.childNodes.length;
    for(var i=0; i &lt; children; i++){
        if(parent.childNodes[i].nodeType != 3){
            if(getChildrensChildren)
                relevantChildren += getCount(parent.childNodes[i],true);
            relevantChildren++;
        }
    }
    return relevantChildren;
}
</code></pre>

<h2>HTML</h2>

<pre><code>&lt;pre&gt;
&lt;div id="test"&gt;
    &lt;span&gt;Apple&lt;/span&gt;
    &lt;span&gt;Apple.&lt;/span&gt;
    &lt;span&gt;Apple&lt;/span&gt;
    &lt;span&gt;Apple&lt;/span&gt;
    &lt;span&gt;Apple&lt;/span&gt;
    &lt;span&gt;Apple&lt;/span&gt;
&lt;/div&gt;
&lt;pre&gt;
&lt;a href="#" onclick="alert(getCount(document.getElementById('test'), false));"&gt;Show counter:&lt;/a&gt;
</code></pre>

<p>Thank you.</p>