Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move a div somewhere else in the dom

Tags:

jquery

The following code gets dynamically inserted into the DOM. However, I'd like to move div#example from where it is and prepend it to #wrapper. How can I use jQuery to achieve this?

<div id="wrapper">
     <div id="div1">
          <div id="example">
          </div>
     </div>
     <div id="div2">

     </div>
</div>

I tried $('#wrapper').prepend('#example');

But that just adds the text #example (not the div) into #wrapper.

like image 474
sean Avatar asked Jul 22 '11 12:07

sean


1 Answers

You could do

$('#wrapper').prepend( $('#example') );

Or

$('#example').prependTo('#wrapper');

like image 121
davin Avatar answered Sep 27 '22 23:09

davin