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?
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.
jQuery clone() Method The clone() method makes a copy of selected elements, including child nodes, text and attributes.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With