'How to show selected value using javascript in laravel

i want to show the selected value to the select option, the value actually selected but cannot show that value on select option form. for example i have a 3 value it can be choose is anyaman, cendana, and bakulan. when 1 edit that form with value cendana, the showed value is the first sequence (anyaman) but when i open that select option that focused on the true data value (cendana).

This is HTML Script :

<div class="mb-1">
     <label class="form-label" for="user-role">Asal Kelompok</label>
       <select id="editkelompok_id" name="kelompok_id" class="select2 form-select">
         <option disabled="" value=""> <b> Pilih asal kelompok </b></option>
            @foreach($kelompok as $data)
              <option value="{{$data->id}}">{{$data->nama_desa}} - {{$data->nama_kelompok}} </option>
            @endforeach
       </select>
 </div>

The Controller

public function detaildataanggota($id)
{
    $status = anggota::findOrfail($id);
    return $status;
}

Java Script

    <script type="text/javascript">
    function detaildataanggota(id){
    $.ajax({
      url: "{{url('admin/pendataan/dataanggota/detail')}}" + "/" + id,
      dataType: "json",
      success: function(status) {
        $('#editid').val(status.id);
        $('#editnama_anggota').val(status.nama_anggota);
        $('#editnik').val(status.nik);
        $('#editjabatan').val(status.jabatan);
        $('#editjenis_kelamin').val(status.jenis_kelamin);
        $('#edittanggal_lahir').val(status.tanggal_lahir);
        $('#editalamat').val(status.alamat);
        $('#editkelompok_id').val(status.kelompok_id);
    },
});
}
</script>

The problem is on #editkelompok_id

enter image description here



Solution 1:[1]

Try this

let select  = $('#editkelompok_id');
select.on('change',()=>{
    // Your stuff
  })

or

let options  = $('#editkelompok_id').children();

    options.each(function() {
        let option = $(this);
        option.on('change', () => {
            select.val(option.val());
        })
    })

I hope it was useful

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1