ditto
ditto

Reputation: 6277

jQuery Cloning Parent Element

I'm wanting to clone a parent element. Currently I've only managed to clone the contents of that element.

$(this).parents('.row').clone();

This returns the contents of .row, how may I clone the .row element?

Fiddle - give the last input a value

Upvotes: 2

Views: 6580

Answers (1)

Jared Farrish
Jared Farrish

Reputation: 49188

What you don't include is that you're using .html() in your jsbin code, which is what's actually returning the inner part of the row.

To "combat" this, use a temporary container and do the .html() on the container:

$('<div>').append($(this).parents('.row').clone()).html();

http://jsbin.com/fesicaqotu/1/edit?js,output

Another, perhaps better option, is to use .outerHTML on the dom element and forget the clone altogether:

self.parents('.row')[0].outerHTML;

http://jsbin.com/buhekitiju/2/edit?js,output

Upvotes: 5

Related Questions