Instantly share code, notes, and snippets.

Embed
What would you like to do?
[JavaScript] When Is jQuery's Clone Function Useful?
<p class="acme-element">
<span>Acme</span>
</p><!--- .acme-element -->
// Loop through each of the image with the custom data-types on the page.
$('img[data-type="acme"]').each(function() {
// ...
});
var $this, $parent, $clone;
// Loop through each of the image with the custom data-types on the page.
$('img[data-type="acme"]').each(function() {
$this = $(this);
$(this).on('click', function() {
// Reference the parent of the image that was clicked and clone the element.
$parent = $this.parent();
$clone = $('.acme-element').clone(true);
// Next, append the cloned element to the parent.
$parent.append($clone);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment