Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move an element in the DOM?

Tags:

I have an element #superWidget in the DOM that is very interactive. It uses jquery UI sortable, draggable, and a bunch of other plugins.

<div id="wrapperA"></div> <div id="wrapperB">   <div id="superWidget"></div> </div> 

I want to change the position of #superWidget in the DOM. I want to remove it from #wrapperB and place it in #wrapperA.

<div id="wrapperA">   <div id="superWidget"></div> </div> <div id="wrapperB"></div> 

I have tried the following...

var copy = $("#superWidget").clone(true, true); $("#superWidget").remove(); $("#wrapperA").append(copy); 

...however this breaks a lot of the plugins.

I do not want to have to rebind everything. Is there a better way to do this? (I notice that jquery UI sortable is somehow able to move elements around in the DOM without breaking any interactivity... there must be a way.)

Thanks (in advance) for your help

like image 821
user1031947 Avatar asked Dec 30 '12 05:12

user1031947


People also ask

How do you move an element into another element?

All you have to do is select the element(s) you want to move, then call an “adding” method such as append() , appendTo() or prepend() to add the selected elements to another parent element.

How do I move a div to another div?

Answer: Use the jQuery . appendTo() Method You can use the jQuery . appendTo() method to move an element into another element.


1 Answers

Rather than duplicating, just do this:

document.getElementById('wrapperA').appendChild(document.getElementById('superWidget')); 
like image 108
Niet the Dark Absol Avatar answered Sep 19 '22 06:09

Niet the Dark Absol