Luca Romagnoli
Luca Romagnoli

Reputation: 12475

Moving a div into another div

i have 2 main div:

    <div id="div1">
<div id="minidiv1">a</div>
<div id="minidiv2">b</div>
   </div>
   <div id="div2"></div>

I want move the minidiv1 into the div2 with jquery

how can i do?

Upvotes: 1

Views: 4400

Answers (4)

skybondsor
skybondsor

Reputation: 747

Luca, did Paul's answer actually work for you? I think you need to be referencing the jQuery object like so:

$($('#minidiv1')).appendTo('#div2'); 
// or
$('#div2').append($('#minidiv1'));

Otherwise, jQuery will just append the string "#minidiv1" to #div2, rather than moving your existing div.

Upvotes: 0

shylent
shylent

Reputation: 10086

$("#minidiv1").appendTo("#div2")

Upvotes: 0

Paul Creasey
Paul Creasey

Reputation: 28884

$('#minidiv1').appendTo('#div2');

or

$('#div2').append($('#minidiv1'));

Upvotes: 0

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827942

You can simply append it to the div2, and it will change its location in the DOM:

$('#minidiv1').appendTo('#div2'); 
// or
$('#div2').append('#minidiv1');

The difference of the above two lines is what is returned, appendTo returns the #minidiv element, append will return the #div2 element.

Use the one you find most useful if you want to make more actions (by chaining).

Upvotes: 5

Related Questions