Skip to content Skip to sidebar Skip to footer

Copy The Content Of A Div Into Another Div

I have two divs on my website. They both share the same css properties, but one of them holds 24 other divs. I want all of these 24 divs to be copied into the other div. This is ho

Solution 1:

Firstly we are assigning divs into variables (optional)

var firstDivContent = document.getElementById('mydiv1');
var secondDivContent = document.getElementById('mydiv2');

Now just assign mydiv1's content to mydiv2.

secondDivContent.innerHTML = firstDivContent.innerHTML;

DEMO http://jsfiddle.net/GCn8j/

COMPLETE CODE

<html>
<head>
  <script type="text/javascript">
    function copyDiv(){
      var firstDivContent = document.getElementById('mydiv1');
      var secondDivContent = document.getElementById('mydiv2');
      secondDivContent.innerHTML = firstDivContent.innerHTML;
    }
  </script>
</head>
<body onload="copyDiv();">
  <div id="mydiv1">
      <div id="div1">
      </div>
      <div id="div2">
      </div>
  </div>

  <div id="mydiv2">
  </div>
</body>
</html>

Solution 2:

You can use the .ready() event of jquery

jQuery(document).ready(function() {
    jQuery('.div1').html(jQuery("#div2").html());
}

Also a working DEMO.


Solution 3:

var divs = document.getElementById('mydiv1').innerHTML;

document.getElementById('mydiv2').innerHTML= divs;

Solution 4:

Alternative in jquery You can use clone and put it in your target div. Example:

$('#mydiv1').clone().appendTo('#mydiv2');

Copy #mydiv1 into #mydiv2


Post a Comment for "Copy The Content Of A Div Into Another Div"