JQuery cloneTo instead of appendTo?

I am trying to use jquery appendTo to copy an element to another container. The object is indeed added, but it is removed from the source. Is there a way to add an element, leaving the original element where it is? name it copy call it clone, name it, as you think.

Here is my current code:

jQuery(document).ready(function()
{
    jQuery('#button').appendTo('#panel'); //button should appear ALSO in panel.
});
+3
source share
3 answers

Close, but not close enough. There will be clone()an element in the code below , then add it to #panel.

jQuery(document).ready(function()
{
    jQuery('#button').clone().appendTo('#panel');
});

Please note that this is not up to standards to IDbe used twice on the same page. So, to fix this:

jQuery(document).ready(function()
{
    jQuery('#button').clone().attr('id', 'newbutton').appendTo('#panel');
});
+9

clone() jQuery:

$('#button').clone().appendTo('#panel');

, , boolean true clone().

, id ... , :

$('#button').clone().attr('id', 'button-clone').appendTo('#panel');
+4

, :

jQuery(document).ready(function()
{
    jQuery('#button').clone().appendTo('#panel'); //button should appear ALSO in panel.
});

, node DOM . (jQuery('#button').clone()), DOM #panel. : DOM, DOM . TONS .

+1

Source: https://habr.com/ru/post/1713807/


All Articles