BlackWolf
BlackWolf

Reputation: 5599

Non-Deep copy of DOM element with jQuery

Is it possible to create a clone/copy of a DOM element in jQuery without cloning its content? I need to split the content of a div into two separate divs with the same attributes. So for example I need to change:

<div class="someclass" someattr="someval">
    this is the first sentence. this is the second sentence.
</div>

into something like:

<div class="someclass" someattr="someval">
    this is the first sentence.
</div>
<div class="someclass" someattr="someval">
    this is the second sentence.
</div>

How exactly the content is split is rather complicated, but this is basically what I need to do. Obviously, creating a clone without content can be achieved using:

$(el).clone().empty();

But since my element can become rather large, I would like to get rid of the overhead of unnecessarily cloning the element content. Ideas? Thanks!

Upvotes: 5

Views: 1792

Answers (1)

1j01
1j01

Reputation: 4180

Just go around jQuery for this operation, as long as you don't need to keep (non-attribute-based) event listeners or other data.

var $clone = $(el.cloneNode(false));

Upvotes: 7

Related Questions