Reputation: 589
I can't access value from a knockout.JS select list
<select data-bind="value: cardType,
optionsCaption: '--',
options: $root.cardTypeList,
optionsText: 'type'"></select>
self.cardTypeList = [{type: 'Visa'},
{type: 'MasterCard'},
{type: 'American Express'}];
self.cardType = ko.observable("").extend({ required: true });
I've tried the following ways to retrieve the value selected, and these are the responses when I used alert to display them.
self.cardType() //displays [object object]
self.cardType //displays a whole bunch of javascript
self.cardTypeList[self.cardType()] //displays undefined
If I select MasterCard from the list and then use alert(self.cardType().toSource())
in firefox it shows ({type:"MasterCard"})
I know it's being set, I just don't know how to access it.
Upvotes: 0
Views: 37
Reputation: 963
Your cardTypeList
is a list of object so, to access selected cardType
you should do,
var yourSelectedCardType = self.cardType().type;
Upvotes: 1