Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created October 24, 2017 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/cdde5f1f6c63c25d981447e0e5874452 to your computer and use it in GitHub Desktop.
Save tommcfarlin/cdde5f1f6c63c25d981447e0e5874452 to your computer and use it in GitHub Desktop.
[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