Knokout : Bind data object with select element's option

Monday, July 7, 2014

I am using KO MVVM for data binding along with Phonegap (running in ripple emulator), but facing issue in on thing. I am trying to bind selected value's object in select element. Here goes my code :


HTML



<select data-bind="options: pro(), optionsText: 'value', optionsValue: this, value: province"></select>


JS



function screen1ViewModel()
{
var self = this;
self.pro = ko.observableArray();
self.province = ko.observable();
}

$(document).ready(function () {
var vm = new screen1ViewModel();


var pro = [{
"symbol": "AB",
"value": "AB - Alberta"
},
{
"symbol": "BC",
"value": "BC - British Columbia"
}];

$.each(pro, function(index, item) {
vm.pro().push(item);
});


});

console.log(vm.province().symbol);


For ex. if I select "AB - Alberta" from select list







http://ift.tt/1rG79F0