I'm trying to display a section that depends on whether one of two radio buttons is selected using knockout's virtual elements with an if statement. The observable isn't being set properly to the value and is blank, so the virtual element is ignored and the inside is always displaying.
HTML
<table style="width:100%" class="table table-striped table-condensed">
<input type="radio" name="aSelect" value="value1" data-bind="checked: aType"/> value1
<input type="radio" name="aSelect" value="value2" data-bind="checked: aType"/> value2
</table>
<!-- ko if: aType() == "value1"-->
<div> THIS SHOULD ONLY SHOW IF VALUE1 RADIO SELECTED</div>
<!-- /ko -->
JAVASCRIPT
function BindingViewModel() {
var self = this;
self.aType = ko.observable("value1");
}
$(function () {
ko.validation.init();
ko.applyBindings(new BindingViewModel());
});
The elements need to be unloaded from the DOM, so visible and hidden bindings won't do, as they only show/hide elements. Any thoughts?
EDIT: Removing ko.validation.init(); and taking the applyBindings outside of the $(function(){}); applies a slight fix. I am able to use the virtual element once, but if I click on a radio button again, it doesn't update. I am using knockout 3.4.0 and knockout.validation 2.0.3.
EDIT 2: Removing the table tag allowed the virtual element to work if the applyBindings is outside of the $(function(){});
EDIT 3: Applying a solution from here fixed it: Bootstrap CSS table-striped not working with knockout if binding. Essentially, I need a tr and td tag inside the virtual element.
EDIT 4: tbody tag may also fix, depending on your circumstance: Knockout Error: Cannot find closing comment tag to match