1
<select id="networkCapacityList" name="networkCapacityList" class="long select2-hidden-accessible" tabindex="-1" aria-hidden="true">
<option value="">Please Select</option>
<option value="221">Business 1</option>
<option value="456">Business 2</option>
<option value="843">Business 3</option>

 $('#networkCapacityList').on('change', function () {
 var businessID = $(this).val();
 var holdBusinessID = localStorage.setItem("businessID", businessID);
 location.reload(); 
});

 getbusinessID = localStorage.getItem("businessID");


 $("#networkCapacityList").find("[value="+getbusinessID+"]").prop("selected", true);
//$("#networkCapacityList option[value="+getbusinessID+"]").prop('selected', true);
//$('#networkCapacityList option').filter('[value="' + getbusinessID + '"]').prop('selected', 'selected');

I am getting a business id from a user action. I hold this data in a localStorage object. I have to reload the page for some functional issues. Then I can get the business id.

After reloading the location, the browser loses all user actions. So, the dropdown list shows "Please select" as a default value. I would like to pre-set the value on the select. I have tried all available options without success. I get the business Id correctly from the localstorage.

For example:

$("#networkCapacityList").find("[value="+getbusinessID+"]").prop("selected", true);
$("#networkCapacityList").find("[value="+221+"]").prop("selected", true);

Any helps would highly be appreciated.

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
pylearner
  • 537
  • 2
  • 8
  • 26
  • Did you try `$("#networkCapacityList").val(getbusinessId);` ? – freedomn-m Dec 13 '18 at 15:17
  • 1
    Possible duplicate of [Set select option 'selected', by value](https://stackoverflow.com/questions/13343566/set-select-option-selected-by-value) – freedomn-m Dec 13 '18 at 15:17
  • @freedomn-m The question is not duplicated. If you read my questions, I have tried all possible solutions, but it did not work as expected. $("#networkCapacityList").find("[value="+getbusinessID+"]").prop("selected", true); //$("#networkCapacityList option[value="+getbusinessID+"]").prop('selected', true); //$('#networkCapacityList option').filter('[value="' + getbusinessID + '"]').prop('selected', 'selected'); All this solutions work on the codepen. But, in my project, it does not work for some weird reasons. – pylearner Dec 13 '18 at 15:57
  • I read your question and you did not provide evidence that you had tried the solution I suggested earlier, so therefore your assertion that you "tried all *possible* solutions" is invalid. – freedomn-m Dec 13 '18 at 16:00
  • @freedomn-m "I have tried all available options without success" – pylearner Dec 13 '18 at 16:01

1 Answers1

0

Just tested your code and its working correctly with jquery 1.11.3, I suspecting the issue may relates to the jquery version... Lets change the code to assign value for the dropdown and try it again:

$("#networkCapacityList").find("[value="+getbusinessID+"]").prop("selected", true);

to

$("#networkCapacityList").val(getbusinessID);
  • Thanks. However, I have already tried these both without success. the project is using select2 plugin, I am not sure, if it something with this. – pylearner Dec 13 '18 at 16:00