How to solved always looping select option value from ajax

I have a data, when I update the data (using modal), select option work correctly
How to solved always looping select option value from ajax

When I close the modal, and I click the edit button again, something wrong with select option:
How to solved always looping select option value from ajax

This is my edit modal ajax:

// Function for edit modal plan schedule
    $('body').on('click', '.editPlanSchedule', function() {
        var Item_id = $(this).data('id');
        $.get("/quotation/getEditPlanSchedule" + '/' + Item_id, function(data) {
            $('.modal-title-edit').html("Edit Plan Schedule Item");
            // $('#product_plan_edit').val(data.product_plan);
            data['product_plan'].forEach(function(item, index) {
                $('#product_plan_edit').append($('<option>', {
                    value: item.productplanID,
                    text: item.productplanID
                if(data['data'].product_plan == item.productplanID){


This is the method from controller:
public function getEditPlanSchedule($id)
    $item['data'] = QuotationPlanSchedule::find($id);
    $item['product_plan'] = ProductPlan::orderby('id', 'asc')->get();
    return response()->json($item);


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You have to clear all options before adding again:


Method 2

Either .empty the container or don’t use append (better for the DOM update)

I am a little confused about your use of data['data'].product_plan to test the ID. In any case you can see the principle

  data['product_plan'].map(item => `<option value="${item.productplanID}">${item.productplanID}</option>`).join('')
$('#product_plan_edit').val(data['data'].product_plan); // select item.productplanID === data['data'].product_plan

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x