I would like to duplicate a <div>
which contains text input and a dropdown.
I have tried the following but it doesn’t copy the input from the dropdown.
Also, how can I duplicate the <div>
but produce empty fields (in both)?
<script>
function myFunction() {
const node = document.getElementById("category_div");
const clone = node.cloneNode(true);
document.body.appendChild(clone);
}
</script>
<div class="category_div" id="category_div">
Project number:
<select>
<option value=""> Select </option>
<option value="1"> 1 </option>
<option value="2"> 2 </option>
</select>
field#2
<input type="text" placeholder="this will be an example..." name="uname" required>
</div>
<p>
<button onclick="myFunction()" class="button btn-duplicate"> + </button>
</p>
2
Answers
Here was my solution to duplicate while maintaining the dropdown input. I'm sure there's a more elegant solution but here's something to build on:
If you want the input to be blank.
We are using the query selector method on our clone to get the input within and then setting its value to ”.
The select menu already is at default for the clone, so that shouldn’t be an issue.