Navin Rauniyar
Navin Rauniyar

Reputation: 10545

how to copy the div to some div and remove original div

Suppose I have the following markup

<div id="test">
  <div class="original">hi</div>
  <div class="news"></div>
  <div class="duplicate"></div>
</div>

Now I want to remove original and paste it to duplicate

The result should look like this

<div id="test">   
  <div class="news"></div>
  <div class="duplicate"><div class="original">hi</div></div>
</div>

I tried like this

var orig = $('.original');
orig.remove().clone(true).appendTo('.duplicate'); // but not working

Upvotes: 0

Views: 133

Answers (3)

George
George

Reputation: 36794

appendTo() will 'move' the element:

var $orig = $('.original');
$orig.appendTo('.duplicate');

JSFiddle

Upvotes: 2

Prusprus
Prusprus

Reputation: 8065

Try this:

var original = $('.original');
var duplicate = $('.duplicate');
duplicate.append(original);

From Jquery API:

If an element selected this way is inserted into a single location elsewhere in the DOM, it will be moved into the target (not cloned):

Demo here

Upvotes: 3

Greenhorn
Greenhorn

Reputation: 1700

You can do this way:

$(".original").detach().appendTo(".duplicate");

Demo Fiddle

Or simply

$(".original").appendTo("duplicate");

Upvotes: 3

Related Questions