UPDATED: I include this codepen becuase I'm having issue with snippet https://codepen.io/ddcreate/pen/RJqOxo
I made this layout using bootstrap 4
What I want to achieve is when click on the "add new item", a new empty row will insert below. Since the site needs to be responsive, it's not possible to put all labels in a row and the fields in another row, otherwise the page will be messed up on smaller screen.
I found some posts about how to clond and insert using jQuery, I tried them but my issue is still here.
I hard code some script, not elegant, and they are not doing exactly what I want. The layout is messed up.
here is the code:
//add empty item fields
let addRow = $('.add-item');
//when click add fields
addRow.on('click', function() {
let newRow = $('.need-to-dup').clone(),
target = $('.need-to-dup').parent();
target[0].append(newRow[0]);
target[1].append(newRow[1]);
target[2].append(newRow[2]);
target[3].append(newRow[3]);
//console.log(newRow, newRow[3])
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="item-form">
<div class="row">
<div class="col-lg-2 col-md-6">
<label for="quanty">Quanty</label>
<input class="need-to-dup" type="text" name="quanty" value="">
</div>
<div class="col-lg-4 col-md-6">
<label for="item">Item</label>
<input class="need-to-dup" type="text" name="item" value="">
</div>
<div class="col-lg-4 col-md-6">
<label for="comment">Comment</label>
<input class="need-to-dup" type="text" name="comment">
</div>
<div class="col-lg-2 col-md-6">
<label for="remove">Remove</label>
<button class="need-to-dup remove-item-button btn-block btn-outline-secondary" type="button" name="remove">
<i class="fa fa-trash-o" aria-hidden="true"></i>
</button>
</div>
</div>
<div class="add-item">
<p>Add Another Item</p>
</div>
</section>