JQuery - Wrap div for a specific element
<div id="last"></div> <p id="text">text</p> <a id="link" href="xxx">link</a> Suppose I have the code above, how can I wrap the <div></div> element in an a element and insert it after the div element, the result should be:
<div id="last"></div> <div><a id="link" href="xxx">link</a></div> <p id="text">text</p> Tried the code below but not working
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.4.4.js"></script> </head> <body> <div id="last"></div> <p id="text">text</p> <a id="link" href="xxx">link</a> <script> $('#link').wrap('<div></div>').detach().insertAfter('#last'); </script> </body> </html> thank you
+4
4 answers
To effectively perform the βCutβ and βPasteβ actions for your element, you want to first transfer it to the DIV tags, disconnect it from the DOM and then paste it back to the target location (ie your βlastβ element)
$('#link').wrap('<div></div>').detach().insertAfter('#last'); 0