innerHTML question

Discussion in 'HTML & Website Design' started by mahmood, Jan 5, 2006.

  1. #1
    Hi guys

    Say I have one div including some text and images. Now I want to put this div inside another using dhtml.

    For example I can put a text inside a div using javascript like this:
    document.getElementById("myDiv").innerHTML = "hey body";
    PHP:
    but the same wouldn't work if I want to put a whole object such as a div like this:
    document.getElementById("myDiv1").innerHTML = document.getElementById("myDiv2");
    PHP:
    Do you think I have to use positioning or is there another way?
     
    mahmood, Jan 5, 2006 IP
  2. torunforever

    torunforever Peon

    Messages:
    414
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You have to consider factors such as whether you want to replace the contents every time you put a div inside the container div. If it's ok to just append every time, then try this simple example.
    <html>
    <head>
    <script type="text/javascript">
    function moveIn(obj)
    {
    	document.getElementById('d1').appendChild(obj);
    }
    </script>
    </head>
    <body>
    <div id="d1" style="border: 1px solid black; width: 50px; height: 50px;"></div>
    <div style="border: 1px solid black; width: 50px; height: 50px;">
    	<div id="d2" style="margin: 10px; border: 1px solid black; background-color: red; width: 10px; height: 10px;" onclick="moveIn(this);"></div>
    	<div id="d3" style="margin: 10px; border: 1px solid black; background-color: blue; width: 10px; height: 10px;" onclick="moveIn(this);"></div>
    </div>
    </body>
    </html>
    Code (markup):
     
    torunforever, Jan 5, 2006 IP
  3. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks torunforever

    Thats what I wanted.
     
    mahmood, Jan 5, 2006 IP