jQuery/JavaScript for-Loop and .append
<p>I started yesterday with jQuery and JavaScript and run into an error.</p>

<p>All I want is to place Ads randomly after an H1-Tag (so let's say there are 10 H1 on a page dived by 2 = 5 AdPosition) now add 5 AdBlocks after a randomly choosen H1.</p>

<p>Sounds pretty easy but I cant achieve this...: / because jQuery or myCode only paste one AdBlock.</p>

<p>JsFiddle: <a href="http://jsfiddle.net/byt3w4rri0r/eCBCK/" rel="nofollow">http://jsfiddle.net/byt3w4rri0r/eCBCK/</a> - working!</p>

<hr>

<p>MyHTML:</p>

<pre><code>&lt;body&gt;
&lt;div id="content-inner"&gt;
    &lt;h1 class="headline"&gt;Headline&lt;/h1&gt;
    &lt;h1 class="headline"&gt;Headline&lt;/h1&gt;
    &lt;h1 class="headline"&gt;Headline&lt;/h1&gt;
    &lt;h1 class="headline"&gt;Headline&lt;/h1&gt;
    &lt;h1 class="headline"&gt;Headline&lt;/h1&gt;
    &lt;h1 class="headline"&gt;Headline&lt;/h1&gt;
    &lt;h1 class="headline"&gt;Headline&lt;/h1&gt;
    &lt;h1 class="headline"&gt;Headline&lt;/h1&gt;
    &lt;h1 class="headline"&gt;Headline&lt;/h1&gt;
    &lt;h1 class="headline"&gt;Headline&lt;/h1&gt;
&lt;/div&gt;
&lt;/body&gt;​
</code></pre>

<hr>

<p>MyJQUERY/JavaScript:</p>

<pre><code>//count H1
var countH1 = parseFloat($("#content-inner h1").length)-1; //-1 because 0 is also a number!

//how often the ads should be displayed
var ad_count = Math.round(countH1 / 2);

//random helper
var min = 0;
var max = countH1;

//random position
for (counter = 0; counter &lt; ad_count; counter++){
    var random_position = Math.round((Math.random() * (max - min)) + min);

    console.log("Random_Position:", random_position);

    // paste google-code

    if ($(".headline").hasClass('advertised') == true) {
        random_position++;

        console.log('already advertised!')

        if (random_position &gt; countH1) {
            random_position--;
            }
    } else {
        $('h1:eq('+random_position+')').append('&lt;div class="google_ad1"&gt;&lt;/div&gt;');
        $('h1.headline').addClass('advertised');
        }
}

console.log("CountH1 -1, because 0=1, 1=2,....", countH1);
console.log(werbung_anzahl);
console.log(zufall_position);
console.log(counter);
</code></pre>
