I need to clone elements according to an input number. So if I select 1, I'll have one cloned element, if I select 2, I'll have two cloned elements.
I've tried with $("elementtoclone").clone().appendTo("clonecontainer") and it work, but just the first time, because when I select another number, it just append another clon. For example, If I select 1, I'll have that one, but after if I select 2 I'll have three but I just need two.
So I've been thinking that the logic way to do this is using the .html() method instead appendTo().
So I need something like this:
function letsClone(times){
var clons;
for(var i=0; i<times; i++){
clons = clons + $("#original").clone();
}
$("#clonecontainer").html(clons);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" onchange="letsClone(this.value)">
<div id="original"><p>I'm gonna be cloned</p></div>
<div id="clonecontainer" style="border:1px solid red">
</div>
But that obviously doesn't work because that's how I add strings, not DOM elements.
So is there a way to add that cloned elements to a variable and then using the .html() method to show them?