Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move child element from one parent to another using jQuery [duplicate]

I am using the jQuery DataTables plugin. I would like to move the search box (.dataTables_filter) and number of records to display dropdown (.dataTables_length) from their parent element (.dataTables_wrapper) to another div on my page without losing any registered javascript behavior. For instance the search box has a function attached to the 'keyup' event and I want to keep that intact.

The DOM looks like this:

<body> <div id="parent1">   <div class="dataTables_wrapper" id="table1_wrapper">     <div class="dataTables_length" id="table1_length">       <select size="1" name="table1_length">         <option value="10">10</option>         <option value="25">25</option>         <option value="50">50</option>         <option value="100">100</option>       </select>     </div>     <div class="dataTables_filter" id="table1_filter">       <input type="text" class="search">     </div>     <table id="table1">     ...     </table>   </div> </div> <div id="parent2">   <ul>     <li><a href="#">Link A</a></li>     <li><a href="#">Link B</a></li>     <li><a href="#">Link C</a></li>   </ul> </div> </body> 

This is what I would like the DOM to look like after the move:

<body> <div id="parent1">   <div class="dataTables_wrapper" id="table1_wrapper">     <table id="table1">     ...     </table>   </div> </div> <div id="parent2">   <div class="dataTables_filter" id="table1_filter">     <input type="text" class="search">   </div>   <div class="dataTables_length" id="table1_length">     <select size="1" name="table1_length">       <option value="10">10</option>       <option value="25">25</option>       <option value="50">50</option>       <option value="100">100</option>     </select>   </div>   <ul>     <li><a href="#">Link A</a></li>     <li><a href="#">Link B</a></li>     <li><a href="#">Link C</a></li>   </ul> </div> </body> 

I've been looking at the .append(), .appendTo(), .prepend() and .prependTo() functions but haven't had any luck with these in practice. I've also looked at the .parent() and .parents() functions, but can't seem to code a workable solution. I have also considered changing the CSS so that the elements are absolutely positioned - but to be frank the page is setup with fluid elements all over, and I really want these elements to be floated in their new parents.

Any help with this is much appreciated.

like image 317
Dom Avatar asked Apr 08 '10 00:04

Dom


People also ask

How do you move an element from one element to another?

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. jQuery automatically realises that the element(s) to add already exist in the page, and it moves the element(s) to the new parent.

Can we get the children element from the parent element using jQuery?

jQuery children() method is used to get the direct children of the selected HTML element. You can use children() method to traverse through the child elements of the selected parent element.

What is the use of parent () and child () method in jQuery?

It is a jQuery Selector used to select all elements that are the direct child of its parent element. Parameter Values: parent: Using this, the parent element will be selected. child: Using this, the direct child element of the specified parent element will be selected.

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.


2 Answers

As Jage's answer removes the element completely, including event handlers and data, I'm adding a simple solution that doesn't do that, thanks to the detach function.

var element = $('#childNode').detach(); $('#parentNode').append(element); 

Edit:

Igor Mukhin suggested an even shorter version in the comments below:

$("#childNode").detach().appendTo("#parentNode");

like image 55
eric.itzhak Avatar answered Sep 29 '22 02:09

eric.itzhak


Detach is unnecessary.

The answer (as of 2013) is simple:

$('#parentNode').append($('#childNode')); 

According to http://api.jquery.com/append/

You can also select an element on the page and insert it into another:

$('.container').append($('h2'));

If an element selected this way is inserted into a single location elsewhere in the DOM, it will be moved into the target (not cloned).

like image 42
JackArbiter Avatar answered Sep 29 '22 01:09

JackArbiter