Basically, I am trying to write some jQuery that will add and duplicate a line of inputs per click, everything works fine except for this.
Problem Illustration:

When you click the "Delete" button, it will leave the "Add Field" button at the bottom, I would like it to return to the line as shown above.
Html:
<form action="javascript:void(0);" method="POST" autocomplete="off"> <input type="submit" value="Submit"> <br> <br> <div class="bigjane"> <button class="add">Add Field</button><input type="text" name="input_0" placeholder="Input1"><input type="text" name="input_0" placeholder="Input2"><input type="text" name="input_0" placeholder="Input3"><input type="button" class="duplicate" value="duplicate"><input type="button" class="remove" value="remove"><br> </div> </form>
JQuery
jQuery(document).ready(function () { 'use strict'; var input = 1, button = ("<button class='add'>Add Field</button>"); $('.add').click(function () { $(this).clone(true).appendTo('form'); $(this).remove(); $('form').append('<input type="text" name="input_' + (input) + '"placeholder="Input1">'); $('form').append('<input type="text" name="input_' + (input) + '"placeholder="Input2">'); $('form').append('<input type="text" name="input_' + (input) + '"placeholder="Input3"><input type="button" class="duplicate" value="duplicate"><input type="button" class="remove" value="remove"><br>'); input = input + 1; }); $('form').on('click', '.remove', function () { $(this).prev('input').remove(); $(this).prev('input').remove(); $(this).prev('input').remove(); $(this).prev('input').remove(); $(this).next('br').remove(); $(this).remove(); input = input - 1; }); $('form').on('click', '.duplicate', function () { $('.add').attr('id', 'add'); $('#add').removeClass().appendTo('form'); $(this).prev().prev().prev('input').clone().appendTo('form'); $(this).prev().prev('input').clone().appendTo('form'); $(this).prev('input').clone().appendTo('form'); $('form').append('<input type="button" class="duplicate" value="duplicate"><input type="button" class="remove" value="remove"><br>'); input = input + 1; }); });
JSFiddle example
I have a feeling that the answer to this question will be somewhat simple, thanks in advance!
source share