-
-
Save tommcfarlin/cdde5f1f6c63c25d981447e0e5874452 to your computer and use it in GitHub Desktop.
[JavaScript] When Is jQuery's Clone Function Useful?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p class="acme-element"> | |
<span>Acme</span> | |
</p><!--- .acme-element --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Loop through each of the image with the custom data-types on the page. | |
$('img[data-type="acme"]').each(function() { | |
// ... | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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