On my page I want to duplicate a select
element when a user clicks on the ADD
button. I have this code below, which you can also see on this jsfiddle.
<select id="sel">
<option>1</option>
<option>2</option>
</select>
<button id="add">ADD</button>
<div id="test"></div>
<script>
var copy = $('#sel').clone().attr('id', '');
$('#add').click(function(){
$('#test').append(copy);
});
</script>
The code should add an equivalent amount of select
elements as the user has clicked the ADD
button. But it only adds one select and seems to be overwriting the initially appended one after the first click of the ADD
button.
What am I doing wrong?