sean
sean

Reputation: 101

Move a div somewhere else in the dom

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.

Upvotes: 10

Views: 12928

Answers (2)

Robert Koritnik
Robert Koritnik

Reputation: 105019

The other way around with selectors

$("#example").prependTo("#wrapper")

Here's a JSFiddle example to prove that it works as expected. If this code doesn't seem to work in your case, than compose your own JSFiddle so we can actually see what's wrong with your code.

Upvotes: 7

davin
davin

Reputation: 45525

You could do

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

Or

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

Upvotes: 12

Related Questions