Stranger
Stranger

Reputation: 862

About knockout.js visible binding and javascript function

I am using knockout.js and am fairly new to it as we all javascript.

So here's my issue. I have a radio button, which when clicked, has to display a div block.

Here's the code for the radio button:

<label class="radio inline">
                    <input type="radio" class="advanced" name="chooseMe" value="chooseMe" data-bind="checked: qrType" />
                    ChooseMe
</label>

Here's the div:

<div  class = "salaryBlock" data-bind="visible: qrType() == 'chooseMe'"><input type="text" id="" placeholder="Salary" /></div>

I have a separate viewmodel file which should contain the javascript method.

Here's the method: $(function () { var rbViewModel = { qrType: ko.observable('plaintext') }; ko.applyBindings(rbViewModel); });

*I have two questions: * 1) How do I tie this function to the div tag with the class name? I am not sure how to write the code. Should it be $(.salaryBlock).function(){ var rbViewModel = { qrType: ko.observable('plaintext') }; ko.applyBindings(rbViewModel); }); `

2) Is my code in html for binding correct? Please let me know.

I would appreciate your help guys.

Upvotes: 0

Views: 1725

Answers (1)

Srikanth Venugopalan
Srikanth Venugopalan

Reputation: 9049

Here is a fiddle - http://jsfiddle.net/kyQcF/1/

You dont need to bind the view model using $function.

Alternatively, you could do what @7zark7's fiddle does. Basically, you need to ensure that when your js file loads, the viewmodel is initialized and ko.applybindings is called with that view model.

Upvotes: 1

Related Questions