I have a code that changes the content of another select option based on the selection of another select option, but the issue is that when an option is picked from the first select option it takes time for the other to auto-populate sometimes it does not populate until users refresh the page and try again.
var $ = jQuery;
var dstvplans = {!! json_encode($dstvplans) !!};
var gotvplans = {!! json_encode($gotvplans) !!};
var startimesplans = {!! json_encode($startimesplans) !!};
$(document).ready(function () {
$("#product").change(function () {
$('#plan').children('option').remove();
var val = $(this).val();
var toAppend='<option value="null">default</option>>';
if (val == "BPD-NGCA-AQA") {
$.each(dstvplans, (index, item) => {
toAppend+=`<option value=${item.id}>${item.name} ${item.price}</option>`;
});
} else if (val == "BPD-NGCA-AQC") {
$.each(gotvplans, (index, item) => {
toAppend+=`<option value=${item.id}>${item.name} ${item.price}</option>`;
});
} else if (val == "BPD-NGCA-AWA") {
$.each(startimesplans, (index, item) => {
toAppend+=`<option value=${item.id}>${item.name} ${item.price}</option>`;
});
}
$("#plan").append(toAppend)
});
});
</script>
note I am getting the content from my backend...here is my backend code
public function index()
{
$cablenetworks = CableNetwork::all();
$dstvplans = CablePlan::where('cablenetwork_id', 1)->get();
$gotvplans = CablePlan::where('cablenetwork_id', 2)->get();
$startimesplans = CablePlan::where('cablenetwork_id', 3)->get();
$transactions = Transaction::where('user_id', Auth::id())
->where('payment_type', 'cable')->orderBy('created_at', 'DESC')->paginate(10);
$gettoken = AirAdmin::all();
foreach ($gettoken as $q) {
$token = $q->token;
}
return view('resellerutility.index', compact('cablenetworks', 'dstvplans', 'gotvplans', 'startimesplans', 'transactions', 'token'));
}
Please serious help is needed on this fast!