Home:ALL Converter>How to set selected option by using JavaScript or jQuery in data-bind select option

How to set selected option by using JavaScript or jQuery in data-bind select option

Ask Time:2021-04-24T02:32:53         Author:Asanka Yaparathna

Json Formatter

I bind-data in select option,

<select 
  id="TanentNamesDB"
  data-bind="
  value:SelectedOrganisationData,
  event: { change: ConnectSelectedOrganization } ,
  options: GridAllOrganisationDataList,
  optionsText:'TenantName',optionsCaption: 'Choose your organisation ...',
  optionsId:'SelectTenantName'
   "
   class="dropdown">
 </select>

It looks like this, enter image description here

And, how to change the selected value using javascript or jquery, When I change the option manually it will automatically update to the database,

I tried different, different way like,

<script type="text/javascript">
    var SelectOp = document.getElementById('TanentNamesDB');
    SelectOp.selectedOption(TanentNameSelected);

    $("#TanentNamesDB option[text='BD']").attr("selected", "selected");
    $("#TanentNamesDB").setSelectedOption("BD");
</script>

Author:Asanka Yaparathna,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/67235226/how-to-set-selected-option-by-using-javascript-or-jquery-in-data-bind-select-opt
yy