I have a modal which share the same data-target. If I clicked the edit button, then close and click the add button, the modal will display the edit modal instead of add modal. How can I change the form?
Add button:-
<button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#modal-new-ingredients" onClick=(setRestaurantId({{ $restorant_id }}))>{{ __('Add') }}</button>
Edit button:-
<button type="button" class="dropdown-item" data-toggle="modal" data-target="#modal-new-ingredients" onClick=(setIngredientsId({{ $bom->id }}))>Edit</button>
setRestaurantId function:-
function setRestaurantId(id){
$('#res_id').val(id);
// $('#form')[0].reset();
// $('#ingredients').trigger('change');
$('#modal-title-new-ingredients').html("Add Ingredient")
$('#ingredients').val("");
$('#consumption').val("");
}
setIngredientsId function:-
function setIngredientsId(id){
ingredients.forEach(element => {
if(element.id==id){
console.log('hello', element.raw_material)
$('#modal-title-new-ingredients').html("Edit Ingredient")
$('#bom_id').val(element.id);
$('#ingredients').val(element.raw_material_id);
$('#consumption').val(element.quantity);
$('#ingredients').trigger('change');
}
});
}
HTML:-
<form role="form" method="post" action="{{ route('bom.store', $item->id) }}" enctype="multipart/form-data" id="form">
@csrf
<div class="form-group">
<label class="form-control-label" id="form-ingredients" for="ingredients">{{ __('Ingredients') }}</label>
<select class="noselecttwo form-control" name="ingredients" id="ingredients" required>
<option disabled selected value> -- Select a Ingredient -- </option>
@foreach ($ingredients as $ingredient)
<option id="var{{$ingredient->id}}" value="{{$ingredient->id}}">{{ $ingredient->name.' - '. $ingredient->rawMaterialUnit->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label class="form-control-label" for="consumption">{{ __('Consumption') }}</label>
<input type="number" step="any" name="consumption" id="consumption" class="form-control" placeholder="{{ __('Consumption') }}" value="" required>
</div>
@include('partials.input',['type'=>"hidden",'name'=>'bom_id','id'=>'bom_id','required'=>false,'placeholder'=>'id'])
<div class="text-center">
<button type="submit" class="btn btn-primary my-4">{{ __('Save') }}</button>
</div>
</form>
Modal HTML for ingredients if needed:-
<div class="modal fade" id="modal-new-ingredients" tabindex="-1" role="dialog" aria-labelledby="modal-form" aria-hidden="true">
