CoolCoder
CoolCoder

Reputation: 124

Knockout binding for toggle switch

I need to bind jquery toggle switch with knockout observables , please help me with correct approach as mine is not working

My html file looks like :

HTML:

<select name="toggleswitch1" id="toggleswitch1" data-theme="b" data-role="slider" data-bind="option:activateNotification">
    <option value="false">No</option>
    <option value="true">Yes</option>

and view model is :

Javascript:

function selectVM(){
    self = this;
    self.activateNotification = ko.observable(true);
}

Upvotes: 2

Views: 587

Answers (2)

Skyp
Skyp

Reputation: 1197

Here is a little fiddle that works : http://jsfiddle.net/9X5j8/

Html :

<select name="toggleswitch1" id="toggleswitch1" 
data-theme="b" data-role="slider" 
data-bind="value:activateNotification">
<option value="false">
     No
</option>
<option value="true">
  Yes
</option>
</select>

    <span data-bind="text :activateNotification"></span>

JS:

function selectVM(){
   self = this;
   self.activateNotification = ko.observable(true);
}

var vm = new selectVM();
ko.applyBindings(vm);

You might want to give the same default value to your observable as the first option of the select.

Upvotes: 1

Claudio Redi
Claudio Redi

Reputation: 68440

Not 100% sure what you're trying to do but if you are trying to bind the selected option you have to use value: activateNotification

Upvotes: 1

Related Questions