Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get html of cloned element

Tags:

jquery

clone

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()?

like image 301
user530753 Avatar asked Jul 25 '11 19:07

user530753


People also ask

What is cloning in HTML?

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

How do you clone HTML 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 duplicate an element in HTML?

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).

How copy HTML using 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.


1 Answers

Try this

var txt=jQuery("input[name="+n+"[]]:first").clone().wrap("<div />").parent().html();
like image 165
ShankarSangoli Avatar answered Sep 23 '22 05:09

ShankarSangoli