Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between .clone() and .html()?

What is the difference between Jquery's .clone() and .html() functions?

Jquery documentation states:

The .clone() method performs a deep copy of the set of matched elements, meaning that it copies the matched elements as well as all of their descendant elements and text nodes.

In an HTML document, .html() can be used to get the contents of any element. If the selector expression matches more than one element, only the first match will have its HTML content returned.

To me these seem to interchangeable, so are there specific situation where one would be used of the other?

like image 787
Mark Avatar asked Jul 24 '14 20:07

Mark


People also ask

What is clone in HTML?

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

What is the difference between clone and copy?

clone - create something new based on something that exists. copying - copy from something that exists to something else (that also already exists).

What is Javascript clone ()?

Cloning. Cloning in javascript is nothing but copying an object properties to another object so as to avoid creation of an object that already exists. There are a few ways to clone a javascript object. 1) Iterating through each property and copy them to a new object.

What is Clone function in jQuery?

The clone() is an inbuilt method in jQuery which is used to make a copy of selected elements including its child nodes, text and attributes. Syntax: $(selector).clone(true|false) Parameter: It accepts an optional parameter which could be either true or false specifies that event handler should be copied or not.


2 Answers

.clone() returns a jQuery object while .html() returns a string.

Use .clone() if you want a copy of the jQuery objects and use .html() to get the inner HTML of a jQuery object in a string format. Each method has its own purpose (and cost).

Also, as sgroves pointed out, ".clone() performs a deep copy of the set of elements found by the selector, while .html() only [uses] the first matched element."*


*Although note that .html(newContent) will set the inner HTML of a set of matched elements. Fiddle

Another note: the (generally) "faster" option is to use strings rather than jQuery objects when doing DOM manipulation (JSPerf). Thus, I'd recommend .html() if all you need is text content. Again though: each method has its own purpose.

like image 64
Casey Falk Avatar answered Oct 11 '22 14:10

Casey Falk


Here are a list of differences :

  1. .clone can be used on multiple selected elements while .html() returns only the html of the first element.

  2. .clone returns a jQuery object while .html returns a string.

  3. .clone can (if specified) keep any event and data of the DOM element. .html will not.

  4. .clone makes a copy of the selected element and all its descendents while .html only gets its descendents. In other words, comparing to DOM methods, .clone is similar to .outerHTML and .html is more like .innerHTML.

like image 25
Karl-André Gagnon Avatar answered Oct 11 '22 15:10

Karl-André Gagnon