This is the template for the dynamic HTML. When I click on the 'Add More', with id plus5, I am trying to get the dynamic form id so that I can select the #location element and can append options to it.
In the dynamic form I have dynamic_form, dynamic_form0, dynamic_form1 and location as location, location0, location1 etc.
$("#dynamic_form #plus5").on('click', function() {
// select the specidic dynamic form id , which can be dynamic_form , dynamic_form0, dynamic_form1 and then select the location and then append options
alert($(this).closest('#dynamic_form'));
});
<div class="form-group" id="dynamic_form">
<div class="row">
<div class="col-md-3">
<select type="text" name="location" id="location" placeholder="Enter Product Name" class="form-control"></select>
</div>
<div class="col-md-3">
<input type="text" class="form-control" name="quantity" id="quantity" placeholder="Enter Product Quantity" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" ;>
</div>
<div class="col-md-4">
<textarea class="form-control" rows="1" name="remarks" placeholder="Enter Remarks" id="remarks"></textarea>
</div>
<div class="button-group">
<a href="javascript:void(0)" class="btn btn-primary" id="plus5">Add More</a>
<a href="javascript:void(0)" class="btn btn-danger" id="minus5">Remove</a>
</div>
</div>
</div>