This is my code working on getting the cookie data and set into a variable json_str and data retrieve from cookie was fine as refer in this image
But then the jquery statement some how cant be function as expected.
<script>
var json_str = getCookie('s8RelatedDepartment');
const myArray = json_str.split(",");
if(myArray.includes("volvo")){
$('select[name=cars] option[value=volvo]').attr('selected','selected');
}
if(myArray.includes("opel")){
$('select[name=cars] option[value=opel]').attr('selected','selected');
}
if(myArray.includes("saab")){
$('select[name=cars] option[value=saab]').attr('selected','selected');
}
if(myArray.includes("audi")){
$('select[name=cars] option[value=audi]').attr('selected','selected');
}
</script>
<select name="cars" class="cars" id="cars" style="width:40%" multiple onclick="test()">
<option value="volvo">volvo</option>
<option value="opel">Opel</option>
<option value="saab">Saab</option>
<option value="audi">Audi</option>
</select>
The select option will not selected automatically after refresh. Any ideal?