0
<div id='one'>
<div id='oneone'>
</div>
</div>
<div id='two'>
</div>

On click of a button i want div with id='oneone' in div with id='two'

 <div id='one'>
    </div>
    <div id='two'>
    <div id='oneone'>
    </div>
    </div>

How do i achieve this using JavaScript , Actually instead of div as a child element i want it to work for images

david raj
  • 153
  • 1
  • 5

2 Answers2

2

In case you have jQuery in your project, you can use the below code:

$(document).ready(function(){

  $("#btnMove").click(function(){
  
    var $div = $("#oneone");    
    $("#two").append($div);   
    
  });
});
#one{
  border: 1px solid blue;
  height: 50px;
  width: 200px;
  }

#two{
  border: 1px solid green;
  height: 50px;
  width: 200px;
  }

#oneone{
  background-color:silver;
  height: 30px;
  width: 200px;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div id='one'>
  <div id='oneone'>
  </div>
</div>

<div id='two'>
</div>

<input type="button" id="btnMove" value="Move container">

Using native JavaScript:

function onMoveButtonClick(){
 var divTobeMoved = document.getElementById("oneone"),
     sourceDiv = document.getElementById("one"),
     targetDiv = document.getElementById("two");

  if(sourceDiv.querySelector("#oneone") != null) {
      sourceDiv.removeChild(divTobeMoved);
      targetDiv.appendChild(divTobeMoved);
   }
 
  }
#one{
      border: 1px solid blue;
      height: 50px;
      width: 200px;
      }

    #two{
      border: 1px solid green;
      height: 50px;
      width: 200px;
      }

    #oneone{
      background-color:silver;
      height: 30px;
      width: 200px;
      }
<div id='one'>
      <div id='oneone'>
      </div>
    </div>

    <div id='two'>
    </div>
<input type="button" onclick="onMoveButtonClick()" value="Move">
Developer
  • 6,240
  • 3
  • 18
  • 24
  • I want it using JavaScript only – david raj Jan 18 '17 at 04:53
  • @davidraj - may be you should be clear on the question what exactly you are trying to achieve. Give the image id instead of div id and the above code should work – Developer Jan 18 '17 at 05:08
  • Ok will check and get back to you thanks for answering and sorry for not being specific – david raj Jan 18 '17 at 05:09
  • 1
    Why use `innerHTML` when you already have a node? Just use `appendChild` instead. Using `innerHTML` will not move the node, it will delete it from one place and make a new one elsewhere, which can have other side-effects (for example, any event handlers would not survive). It will also change the identity of the node, which might matter if you use the node's identity elsewhere in code. – Amadan Jan 18 '17 at 05:27
  • @Amadan - thats right, will update my code. thank you – Developer Jan 18 '17 at 05:46
1

Do this:

var parent = document.getElementById('one');
var oneone = document.getElementById('oneone');
var two = document.getElementById('two');
two.appendChild(parent.removeChild(oneone));

removeChild returns the deleted node, use its reference to append at desired place. Wrap it in a function and call it from any event but make sure DOM is loaded before calling this piece of code.

JVM
  • 946
  • 1
  • 10
  • 23