Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone a <tr> element without data but only the structure by using jQuery?

Is there a quick way to clone a <tr> element without its content in cells? Basically to have a pure <tr> element having only the same structure as the original?

like image 745
pencilCake Avatar asked Jun 01 '11 08:06

pencilCake


People also ask

How do you clone an object in jQuery?

To clone an element using jQuery, use the jQuery. clone() method. The clone() method clones matched DOM Elements and select the clones. This is useful for moving copies of the elements to another location in the DOM.

What is the use of clone method in jQuery?

jQuery clone() Method The clone() method makes a copy of selected elements, including child nodes, text and attributes.

How do you duplicate an element in Javascript?

First, select the <ul> with the id menu by using the querySelector() method. Second, create a deep clone of the <ul> element using the cloneNode() method. Third, change the id of the cloned element to avoid duplicate. Finally, append the cloned element to the child nodes of the document.

How do you replace an element with another in jQuery?

We can replace HTML elements using the jQuery . replaceWith() method. With the jQuery replaceWith() method, we can replace each element in the set of matched elements with the provided new content and return the set of elements that were removed.


1 Answers

If you want a deep clone of the element without the text content, you can write something like:

var $cloned = $("tr").clone().children().text("").end();
like image 73
Frédéric Hamidi Avatar answered Sep 20 '22 15:09

Frédéric Hamidi