knockoutjs how to get the selected option arrayObject
by mrrodd
HTML
<select data-bind="options: availableCountries,
optionsText: 'countryName',
value: selectedCountry,
optionsCaption: 'Select...'"></select>
JavaScript
// Constructor for an object with two properties
var Country = function (name, population) {
this.countryName = name;
this.countryPopulation = population;
};
var viewModel = {
availableCountries: ko.observableArray([
new Country("UK", 65000000),
new Country("USA", 320000000),
new Country("Sweden", 29000000)]),
selectedCountry: ko.observable(), // Nothing selected by default
};
viewModel.selectedCountry.subscribe(function (data) {
console.log(data)
});
ko.applyBindings(viewModel);