Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8613575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:54:43+00:00 2026-06-12T04:54:43+00:00

I started yesterday with jQuery and JavaScript and run into an error. All I

  • 0

I started yesterday with jQuery and JavaScript and run into an error.

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.

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

JsFiddle: http://jsfiddle.net/byt3w4rri0r/eCBCK/ – working!


MyHTML:

<body>
<div id="content-inner">
    <h1 class="headline">Headline</h1>
    <h1 class="headline">Headline</h1>
    <h1 class="headline">Headline</h1>
    <h1 class="headline">Headline</h1>
    <h1 class="headline">Headline</h1>
    <h1 class="headline">Headline</h1>
    <h1 class="headline">Headline</h1>
    <h1 class="headline">Headline</h1>
    <h1 class="headline">Headline</h1>
    <h1 class="headline">Headline</h1>
</div>
</body>​

MyJQUERY/JavaScript:

//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 < 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 > countH1) {
            random_position--;
            }
    } else {
        $('h1:eq('+random_position+')').append('<div class="google_ad1"></div>');
        $('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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:54:44+00:00Added an answer on June 12, 2026 at 4:54 am

    With this line

    if ($(".headline").hasClass('advertised') == true)
    

    you are checking if any(!) of the .headline elements has already been advertised. And after the first step there is always one .headline that has been advertised, so this comparison will be true.

    What you wanted to do: Check if the .headline with the current index has been advertised

    if ($(".headline").eq(random_position).hasClass('advertised') == true)
    

    On top of this, you are adding the class “advertised” to all(!) your h1.headline elements. This is what you wanted to do instead

    $('h1.headline').eq(random_position).addClass('advertised');
    

    EDIT: This is the full changed for loop from the latest fiddle at http://jsfiddle.net/eCBCK/19

    for (counter = 0; counter < ad_count; counter++){
        var random_position = Math.round((Math.random() * (max - min)) + min);
    
        console.log("Random_Position:", random_position);
    
        // paste google-code
    
        if ($(".headline").eq(random_position).hasClass('advertised') == true) {
            counter--;
    
            console.log('already advertised!')
    
    
        } else {
            $('h1:eq('+random_position+')').append('<div class="google_ad1"></div>');
            $('h1.headline').eq(random_position).addClass('advertised');
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started looking into jQuery yesterday, before this I programmed in only PHP.
I converted all my html files to jquery yesterday and page requests started being
I started looking into PhoneGap yesterday and created a simple marble rolling around while
Yesterday I have started developing my first Metro style App using JavaScript. I've used
I am working on my personal website and am learning javascript (started yesterday) -
I started learning jQuery just yesterday, and I like it very much. And now
First of all, I am new to R (I started yesterday). I have two
I started studying jQuery yesterday by doing tutorials well I'm on tutorial 28 /200
I am trying to make something, and I started reading about jQuery just yesterday,
Started learning javascript yesterday at CodeAcademy and decided I could actually write code. Just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.