2

I'd like to use jQuery.clone() but I only want to copy the inner elements, not the element itself. If needed I can wrap the contents but I was hoping to find another way to just clone the inner html.

Thanks.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
fancy
  • 48,619
  • 62
  • 153
  • 231

2 Answers2

3

Yup, you can use .children().clone()

http://jsfiddle.net/pR6ve/1

Naftali
  • 144,921
  • 39
  • 244
  • 303
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
2

You need to use contents to get all the child nodes (and their descendants):

$('#foo').contents().clone(true).appendTo('#bar');
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • yet another `$("#foo").clone(true).unwrap().appendTo("#bar");` – Andreas Louv Dec 15 '11 at 22:44
  • @AndreasAL No, because that would attempt to unwrap the clone of `#foo`, which is impossible because it doesn't have a parent element. You'd have to do `.clone(true).contents().unwrap()`. – lonesomeday Dec 15 '11 at 22:49