I’m trying to create an “add more” feature to a web form, using something @PaulOB showed a few years ago. However, whenever the button is pressed it tries to submit the form despite having preventDefault(). I’m using the add more on a date field rather than a file field but I don’t think that should make any difference? It seems to be okay if I put the code into Codepen but then I guess Codepen doesn’t do a form submit.
This is my js:
const addMore = document.querySelector(".add-more");
let fInput = document.querySelector(".more-date");
addMore.addEventListener("click", function (e) {
e.preventDefault();
let newFInput = fInput.cloneNode();
fInput.parentNode.insertBefore(newFInput, fInput.nextSibling);
fInput = newFInput;
});
Voila my HTML form (part thereof):
<form method="post"
<div class="field">
<label for="event">Event:</label>
<input type="text" name="event">
</div>
<div class="field">
<label for="date">Date:</label>
<input type="date" name="date[]" class="more-date" required>
</div>
<div>
<button class="add-more">Add date...</button>
</div>
<input type="submit" value="Make Booking">
</form>