I have an input element like: <input class="input-m" name="formelement[]" type="text">
I want to clone it and store cloned html to a variable:
var txt=jQuery("input[name="+n+"[]]:first").clone().html();
this returns an empty string.
How can I get the html content from .clone()
?
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.
The cloneNode() method creates a copy of a node, and returns the clone. The cloneNode() method clones all attributes and their values. Set the deep parameter to true if you also want to clone descendants (children).
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.
Try this
var txt=jQuery("input[name="+n+"[]]:first").clone().wrap("<div />").parent().html();
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