I am using Laravel 6 and I am making a modal to fetch the info from another related table, the information is printed correctly but if I close the modal and reopen it, the info of said related table is duplicated
Modal:
<div class="modal fade" id="modal_view_shop" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="title_modal_view_shop"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label for="services" class="col-md-4 col-form-label text-md-right">Servicios*</label>
<div class="col-md-6">
<textarea id="services_list" class="form-control"></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal"> Cerrar <i class="fa fa-close"></i></button>
</div>
</div>
</div>
</div>
This is how I get the information:
let services = data.service_shops;
services.forEach(element => document.getElementById('services_list').innerHTML += element.service.title);
$('#modal_view_shop').modal('show');
service_shops is the other table and to access the rest I had to do that forEach
Image of what it looks like when I open the modal for the first time:
image of what it looks like if I close the modal and reopen it
and if I keep opening and closing different modals, the info of the images appears next to that of the modal that I opened
I just had to initialize
#services_list
before iterating thearray
.