Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Completely cut and paste an element

Can I cut (completely, with all styles and attributes) an element from a location and paste it in another location (like body) with jQuery?

Also, I want to place a string (or tag) in the old location, because I want to change its place to old location at the end of the script.

Can I? How?

like image 947
mrdaliri Avatar asked Aug 25 '11 07:08

mrdaliri


2 Answers

appendTo() will automatically move the matched elements from their current location to the specified container, which seems to be what you want.

You can use after() to insert new content before moving the element:

$("#yourElement").after("<p>Element was there</p>").appendTo("body"); 
like image 139
Frédéric Hamidi Avatar answered Sep 25 '22 08:09

Frédéric Hamidi


.detach() is more appropriate for this task, as @lvan suggested.

jQuery(jQuery("#yourElement").detach()).appendTo("body"); 
like image 42
kamal pal Avatar answered Sep 22 '22 08:09

kamal pal