So I have this code when user selects an office
in the select option, another select box loads the employees
in that office
.
My Problem is how can I store or retain the old selected value
from the employee when the 'Confirm' button is pressed and the page loads and then use that old value
to select again the same current value
if the office is not changed?
Here is the code:
$( document ).ready(function() {
var $option = $('#office_id').find('option:selected');
var value = $option.val();
updateSalesDay(value);
$('#office_id').change(function(e){
var $option = $(this).find('option:selected');
//Added with the EDIT
var value = $option.val();
updateSalesDay(value);
console.log(value);
});
});
function updateSalesDay(office_id){
$('.load-bar').css('display','block');
$('.panel-body').css('pointer-events','none');
$('#employee_id').empty();
$.ajax({
type: "POST",
headers: { 'X-CSRF-TOKEN': $('[name="csrf-token"]').attr('content') },
url: "{{ route('postFilterEmployee') }}",
data: {
'office_id': office_id
},
dataType: 'json',
success: function (data) {
console.log(data);
employeesList = data;
if (employeesList.length === 0) {
$('#employee_id').append('<option></option>');
}else{
$('#employee_id').append('<option></option>');
for (index = 0; index < employeesList.length; index++) {
$('#employee_id').append('<option value="'+employeesList[index].id+'">'+employeesList[index].last_name+' '+employeesList[index].first_name+'</option>');
}
}
$('.load-bar').css('display','none');
$('.panel-body').css('pointer-events','');
},
error: function (data) {
console.log(data);
}
});
}
Any ajax masters here? Please help.