Javascript – How to set the initial default value of dropdown in knockout js

drop-down-menujavascriptknockout.js

Here is my code in View

<select data-bind="options:TypeInfo,optionsText: 'PrefName',
        optionsValue: 'PrefName',value: $data.selectedchoice,event:
        {change:gettrivialtable}" class="forms-dropdown"></select>

Here is my Js Code

viewModel = ko.mapping.fromJS(viewData);

self.TypeInfo = viewModel.TypeInfo;

self.selectedchoice = ko.observable('');

Note : viewData is a Json Pushed from model.PrefName is a property of the TypeInfo class. The value returned are 0:'x',1:'y',2:'select' in the same order I want 'select' to be the default value. Please help me out.

Best Solution

you could set your selectedchoice with variable from TypeInfo like:

self.selectedchoice(self.TypeInfo()[0])

or very similar