kailash19
kailash19

Reputation: 1821

Move all contents after a particular div under a different tag

I want to move all the text(html content) after a div(which is again under a div) to another tag. For example, if i have a page like below, i want to move everything after div2 to body:

<body>
    <div id=div1>
      <div id=div2></div>
      <div id=div3></div>
      <script>blah</script>
      and much more
    </div>
</body>

to

<body>
   <div id=div1>
      <div id=div2></div>
    </div>
    <div id=div3></div>
    <script>blah</script>
    and much more
 </body>

How can i do this? Using

$('#div').appendTo('body');

only moves that div.

Upvotes: 0

Views: 129

Answers (2)

&#193;xel Costas Pena
&#193;xel Costas Pena

Reputation: 6235

As of my tries, response above isn't correct because it doesn't move the text nodes. Try this, hope it's what you are looking for:

​var $div = $("#div2"​), $con = $div.parent().contents();
$con.slice($con.index($div)+1).appendTo("body");
​

jsfiddle

Upvotes: 0

Anoop
Anoop

Reputation: 23208

You can achieve this using jQuery.

   var elems = $("#div2").nextAll();
    $('body').append(elems );

jsfiddle

Upvotes: 2

Related Questions