I have a Laravel 9 project where i manage Add, Edit & Delete of Sub Categories using Bootstrap Modal. In Edit Modal list of Categories presented as dropdown menu options added by help of jQuery. When click on Edit button i have call editSubCategory function but I can not specify the selected option in dropdown menu base of saved value in database basically this should be when option.val() == selectedCategoryID when this true we should make this option selected so i have try this after adding options to editSubCategoryModal through jQuery by using the code below but it look like return something not understand it S.fn.init [prevObject: S.fn.init(1)] So i need help to understand how thing should work correctly and this is my current code:
function editSubCategory(e) {
let subCategoryId = e.id;
let subCategoryName = e.sub_category_name;
let selectedCategoryID = e.category_id;
$("#subcategoryid_efield").val(subCategoryId);
$("#subcategoryname_efield").val(subCategoryName);
let url = window.location.origin;
let path = url + "/getcategories";
axios
.get(path)
.then((res) => {
$("#categoryid_efield").html(
'<option value="" disabled>--Select Category--</option>'
);
$.each(res.data, function (key, value) {
$("#categoryid_efield").append(
'<option value="' +
value.id +
'">' +
value.category_name +
"</option>"
);
});
})
.catch((err) => console.log(err));
$("#categoryid_efield option[value='" + selectedCategoryID + "']").attr(
"selected",
"selected"
);
$("#editSubCategoryModal").modal("show");
}
<div class="edit">
<button class="btn btn-sm btn-success edit-item-btn" onclick="editSubCategory({{ $subcategory }})">Edit</button>
</div>
<!-- Edit Modal -->
<div class="modal fade" id="editSubCategoryModal" tabindex="-1" aria-labelledby="editSubModalLabel"
aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header bg-light p-3">
<h5 class="modal-title" id="editSubModalLabel">Edit Sub Category</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"
id="close-modal"></button>
</div>
<form id="EditForm">
@csrf
<div class="modal-body">
<input type="hidden" name="subcategoryid_efield" id="subcategoryid_efield">
<div class="mb-3 form-group">
<label for="categoryid_efield" class="form-label">Category Name</label>
<select class="form-select mb-3" aria-label="Default select example" id="categoryid_efield">
</select>
</div>
<div class="mb-3 form-group">
<label for="subcategoryname_efield" class="form-label">Sub Category Name</label>
<input type="text" name="subcategoryname_efield" id="subcategoryname_efield" class="form-control" placeholder="Enter Sub Category Name" />
</div>
</div>
<div class="modal-footer">
<div class="hstack gap-2 justify-content-end">
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success" id="edit-btn">Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
<!-- End Edit Modal -->