How to dynamically position a div with content within a dynamically created div?

Asked

Viewed 417 times

1

On my website, I create a div dynamically, after the second paragraph (forum).

I’d like to position a banner already loaded inside that div.

tried several ways, to give a apendchild, but it won’t.

Here is the site URL to understand:

Below the code I’m using to create the div:

var meupost = document.getElementsByClassName("post entry-content ")[0];
var meudiv = meupost.getElementsByTagName("p")[1];
var meubanner = document.createElement("div");
meubanner.id = "banner300x250-1-auto";
meubanner.setAttribute('class','ipsBox_container');
meubanner.style.height = "250px";
meubanner.style.width = "300px";
//meubanner.style.position = "relative";
meubanner.style.float= "right";
meupost.insertBefore(meubanner, meudiv);
  • What do you want to put there? It would be the #google_image_div? It would also be good to show what you tried to do (and it didn’t work out), so we can see what’s wrong.

  • 1

    P.S. I tried to simply select any element and put it on div created using appendChild and worked normally: http://jsfiddle.net/99mshwn8/

1 answer

1

Dude, at runtime, he’s not going to add why at the time he’s reading the DOM, he’s not going to find the element you’re identifying to give the append. For this to work, this div should already be in the DOM by the time javascript starts the execution.. Then dynamically, fill the div, and add a class that defines the div styles.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.