How to clone the html code for pasting?

There is a need to save the html code into a variable, modify it, and then clicking the button to insert an unlimited number of times. Tried to do it using the jquery method .clone(), but I don't quite understand how it works, and more than one time code is not inserted demo.
I know that it is possible each time you press the button to call my function createTemplate and all works, but it's a pointless job for the computer because with the first call of the cloned element will not change.
April 4th 20 at 00:32
1 answer
April 4th 20 at 00:34
Solution
Why exotic method one in your code? :)
Here it is in one line it works as it should. Just add the pattern Id instead of a class.

$('button').on('click', function(){
$('.container').append($('#html-to-copy').clone().addClass('modify'));
});


Let me explain for clarity. F-tsiya appendTo (like append ) MOVES the element. In your code with one - once you clone the element and place it in variable to use then many times, but this is a mistake. If you want a lot of templateS , then clone them each time. Otherwise, you have only ONE clone, sootvestvenno crowd of sheep Dolly so not to get :) .
Why exotic one method in your code? :)

To call the function only once and only after clicking the button, the execution result is not needed.

If you want a lot of templateS , then clone them each time.

So in that sense - I want to make the cloned element only once, because the final result every time the same.
That is, I want to get such result https://jsfiddle.net/copq608z/but below is the code for the template variable was taken from the html code, plus it need to change a little before inserting. - enoch.Harber commented on April 4th 20 at 00:37
https://jsfiddle.net/copq608z/1/
In this form it works, but looks like bad code - enoch.Harber commented on April 4th 20 at 00:40
@enoch.Harber,
what you do not like ?
$('button').on('click', function(){
$('.container').append($('#html-to-copy').clone().addClass('modify'));
});


do not save on matches.
On the one hand
a meaningless job for a computer
on the other hand use a class selector for the template. Despite the fact that element one :). Well, if you do computer and want to create a template once, then you can so

var template;

function createTemplate() {
 template = $('#html-to-copy').clone().addClass('modify').html();
}

$('button')
 .one('click', createTemplate)
 .on('click', function(){
$('.container').append(template);
})
- lyla_Da commented on April 4th 20 at 00:43
do not save on matches.

It's not saving, I want to understand how to implement it in the best way.

Your last option I like, thank you! - enoch.Harber commented on April 4th 20 at 00:46
@enoch.Harber, ))) good luck. View will shift tools like vue.js .There's templates out of the box. - lyla_Da commented on April 4th 20 at 00:49

Find more questions by tags JavaScriptjQuery