I have a form in html page, with 3 inputs, I would like to have a add-form button , when I click , the 3 input are cloned, between the first input group and the submit button.
Actually, when I click, the all form seems cloned, so I have 2 times add-form button and submit button.
var clone = $('#form-container').html();
$('#add-form').click(function() {
$('#form-container').append(clone);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<form id="form-container" method="GET">
<table>
<div class="table-form">
<label for="Field">Field </label>
<select name="field" class="form-control">
<option value="opt">label</option>
</select>
<label for="lookup">Lookup </label>
<select name="lookup" class="form-control">
<option value="iexact">iexact</option>
<option value="icontains">icontains</option>
<option value="in">in</option>
</select>
<label for="value">Value </label>
<input id="value" type="text" name="value" value="current_value">
</div>
</table>
<button id="add-form" type="button" class="btn btn-primary">Add form</button>
<button type="submit" class="btn btn-primary">Search</button>
<button type="submit" name="btn-reset" class="btn btn-secondary" value="btn-reset">Reset</button>
</form>
2
Answers
Note I removed the invalid table tag and changed type="submit" to type="reset" on the reset button
If you only want the content of the table-form to be duplicated, then we can do this instead
If, however you want to CLONE the div, then this code will create a new clone each time since if you only store a clone outside the click, it will be the same element that is moved (appended) each time. It makes more sense to have a set inside a div in the form than than to add the elements to one div
You need to clone only the section that you want to clone and append it in
#form-container'
You can use like