简体   繁体   中英

clone all element and content inside div and display it into another div

Get all elements inside a particular div and display the the same to other div. the content inside is dynamic . I want to clone all the elements inside ie

<div id="div_preview"> 
  <br>
  2 php Developer ?<br>
  <input type="checkbox" name="chk_0" value="a">
  a
  <input type="checkbox" name="chk_1" value="b">
  b
  <input type="checkbox" name="chk_2" value="c">
  c 
  </div>

from <div id="div_preview">  and display it to <div id='other_div'></div>

so that o/p

 <br>
      2 php Developer ?<br>
      <input type="checkbox" name="chk_0" value="a">
      a
      <input type="checkbox" name="chk_1" value="b">
      b
      <input type="checkbox" name="chk_2" value="c">
      c 
document.getElementById('other_div').innerHTML = document.getElementById('div_preview').innerHTML;

Displays in alert:

$(document).ready(function(){
        alert($('#div_preview').html());    

    });
$("button").click(function{
var result=$('#div_preview').html();
$("other_div").html(result);
});

You can use the contents for that - just clone the contents and then add the cloned elements to the new container.

Here is an example:

 $('#div_preview').contents().clone().appendTo('#div_container')
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="div_preview"> <br> <input type="checkbox" name="chk_0" value="a"> a <input type="checkbox" name="chk_1" value="b"> b <input type="checkbox" name="chk_2" value="c"> c </div> <br /> <div id="div_container"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM