I am working on a form that allows the user to add additional input fields by clicking a button.
It basically looks like this:
<div>
<input type="text" placeholder="existing"/>
</div>
<button class="add">add</button>
With a little JavaScript:
var add = document.querySelector(".add");
var div = document.querySelector("div");
add.addEventListener('click', function () {
div.innerHTML += '<input type="text" placeholder="new"/>';
});
However, I noticed that when the button is clicked - if any existing input fields have values, they are cleared.
I have been doing this for a while and can't find a solution to stop this, so I wonder if anyone here can help.
Here's the script http://jsfiddle.net/gxzLZ/
source
share