views:

43

answers:

2

We have two divs. divOne contains information of image and divTwo contains information of table. Is it possible that we can set divOne as background image of divTwo using script?

divOne.innerHTML = <IMG src="http://sbc.com/xyz.jpg" />

divTwo.innerHTML = <TABLE id="content"> ....... </TABLE>
A: 

divOne.style.backgroundImage="url(http://sbc.com/xyz.jpg);";

That should set divOne to that background image...


For the div Positioning give this a try...

divOne.style.position = "relative";
divOne.style.top = "0px";
divOne.style.left = "0px";
divOne.style.zIndex="1";

divTwo.style.position = "relative";
divTwo.style.top = "0px";
divTwo.style.left = "0px";
divTwo.style.zIndex="2";
Isisagate
That is assuming you are doing this... <div id="divOne"><div id="divTwo"></div></div>
Isisagate
Thanks for your response. We have url inside div which is generated dynamically. Right now, we are doing divOne.appendChild(div2) but this display first div and then below it second div. We want to display first div as background of second div. Thanks again.
Malik
divOne.style.position = "relative";divOne.style.top = "0px";divOne.style.left = "0px";divOne.style.zIndex="1";divTwo.style.position = "relative";divTwo.style.top = "0px";divTwo.style.left = "0px";divTwo.style.zIndex="2";
Isisagate
Thanks for your response but not working. DivOne (background image) disappeared.
Malik
can you post some of your code so it will be easier to give an exact solution? is divTwo transparent?divTwo.style.backgroundColor = "transparent";I think that might fix it...
Isisagate
Thanks for you response. Removed - divOne.style.top = "0px"; divOne.style.left = "0px"; divTwo.style.top = "0px"; divTwo.style.left = "0px"; and changed divOne.style.position = "absolute"; divTwo.style.position = "absolute". It worked. Thanks again for all your help.
Malik
+1  A: 

Yes - depending on what you mean.

You can position one DIV over another using relative positions and z-index. If the top index is transparent (no background) then you should be able to see through to the one behind it.

michael
Thanks for your response. We are thinking the same but unable to find any example. Any help?
Malik