Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 how it looks like:

<div id="mydiv1">
    <div id="div1">
    </div>
    </div id="div2>
    </div>
    //and all the way up to div 24.
</div>

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

I want all of the 24 divs within mydiv1 to be copied into mydiv2 when the page loads.

Thanks in advance

like image 942
Hwende Avatar asked Nov 24 '13 09:11

Hwende


People also ask

How do I clone a div?

To clone a div and change its id with JavaScript, we can use the cloneNode method on the div. Then we can set the id property of the cloned element to set the ID. to add the div.

How do I copy text inside a div?

The function copies the visible text of the element to the clipboard. This works as if you had selected the text and copied it with ctrl+c. Use the parameter "id" to select the element you want to copy.

How do I move a div to another div using jQuery?

All you have to do is select the element(s) you want to move, then call an “adding” method such as append() , appendTo() or prepend() to add the selected elements to another parent element. jQuery automatically realises that the element(s) to add already exist in the page, and it moves the element(s) to the new parent.

How do you copy an element in HTML?

You call the cloneNode() method on the element you want to copy. If you want to also copy elements nested inside it, pass in true as an argument. // Get the element var elem = document. querySelector('#elem1'); // Create a copy of it var clone = elem.


Video Answer


3 Answers

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>
like image 149
Aycan Yaşıt Avatar answered Oct 06 '22 04:10

Aycan Yaşıt


You can use the .ready() event of jquery

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

Also a working DEMO.

like image 28
Rahul Tripathi Avatar answered Oct 06 '22 02:10

Rahul Tripathi


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

document.getElementById('mydiv2').innerHTML= divs;
like image 42
Ankit Tyagi Avatar answered Oct 06 '22 02:10

Ankit Tyagi