Note that there are some explanatory texts on larger screens.

plurals
  1. POLabelling Checkbox and Radio in knockout
    primarykey
    data
    text
    <p>In html, when we click on the text or hover over the text of a radio button or checkbox, we are able to select it with the html as shown below:</p> <pre class="lang-html prettyprint-override"><code>&lt;label&gt; &lt;input type="checkbox" /&gt;option 1 &lt;/label&gt; </code></pre> <p>OR</p> <pre class="lang-html prettyprint-override"><code>&lt;input id="checkboxid" type="checkbox" /&gt; &lt;label for="checkboxid"&gt;option 1&lt;/label&gt; </code></pre> <p>I am trying to get the same behavior with knockout, but unable to find much help on the same:</p> <pre class="lang-html prettyprint-override"><code>&lt;label data-bind="text: $data.optiontext"&gt; &lt;input type="checkbox" data-bind="value: $data.optionvalue, checked: $parent.selectedOptions" /&gt; &lt;/label&gt; </code></pre> <p>The View (or html) is below (Note the below code does not contain the above html, so when you try it you will need to make the above changes and check it):</p> <pre class="lang-html prettyprint-override"><code>&lt;div data-bind="foreach: options"&gt; &lt;input type="checkbox" data-bind="value: $data.optionvalue, checked: $parent.selectedOptions" /&gt; &lt;label data-bind="text: $data.optiontext"&gt;&lt;/label&gt; &lt;/div&gt; &lt;hr /&gt; &lt;div data-bind="text: selectedOptionsList"&gt;&lt;/div&gt;​ </code></pre> <p>Here is the view model:</p> <pre><code>var viewModel = { options: [ { optiontext: 'Simple', optionvalue: "1" }, { optiontext: 'Advanced', optionvalue: "2" } ], selectedOptions: ko.observableArray(["2"]) }; viewModel.selectedOptionsList = ko.computed(function() { return this.selectedOptions().join(","); }, viewModel); ko.applyBindings(viewModel); </code></pre> <p>Here is the jsFiddle link: <a href="http://jsfiddle.net/rupesh_kokal/AFzbY/" rel="nofollow noreferrer">http://jsfiddle.net/rupesh_kokal/AFzbY/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload