2

I have a dropdown in my edit form, but it doesn't display the selected value on the index.

Dropdown box image

 var ingredients=<?php echo $boms->load('rawMaterial') ?>;
 console.log('BillOfMaterials', ingredients);

enter image description here

I put onClick=setIngredientsId on my edit button.

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);
            }
        });
    }

Console log result for hello

Below is my dropdown HTML:-

<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>
4
  • 2
    I'm pretty sure you only miss this: ` $('#ingredients').trigger('change');` This way you let JS update the UI Commented Jun 1, 2021 at 10:18
  • $('ingredients option[value=' + element.raw_material_id + ']').prop('selected',true); ? Commented Jun 1, 2021 at 10:22
  • @RobBiermann yes, finally! Only missed a single line, thanks a lot! :) Commented Jun 1, 2021 at 12:56
  • Hey @Shree, I guess yours is correct too but it doesn't work on my code. Anyway, the answer from Rob solve the problem. Thanks to you too! :) Commented Jun 1, 2021 at 12:59

1 Answer 1

1

Only missed a single line, here for those who face the same problem!

$('#ingredients').trigger('change');

This way you let JS update the UI. Thanks Rob for helping at the comment section!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.