Last Updated: February 25, 2016
·
1.471K
· ytilis

Clone limitations in jQuery

Creating a virtual object in JQuery via the clone method makes some functions unable to interact with it.

For example, the following will work as expected, adding the class to the clone and not the original:

$('.myClass').clone().addClass('.myOtherClass');

The following, however, will NOT work:

$('.myClass').clone().wrap("<div class='container'></div>");

As a result, the clone will remain unchanged. If you want to use wrap, and other functions with this limitations, you’ll need to place it somewhere in the DOM first, and then manipulate it.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside