Reputation: 1550
Let's say I have an Html element
<element testattribute='a'></element>
I know I can pass the value of testattribute using a binding like this:
<element data-bind="attr: { testattribute: 'a'}></element>
But how can I completely remove this attribute through the binding. So if the original value was
<element testattribute: 'a'></element>
my end result should be
<element></element>
Upvotes: 15
Views: 4386
Reputation: 17926
you should do this ternary inline because form not accepts several checked radio button and it will ignore the knockout comments so none would be checked to avoid do like:
<input type="radio" data-bind="attr:{'checked':($data.amount > 0) ? 'checked' : false}" id="q156" name="foo" value="positive" />
this will set accurate!
Upvotes: 3
Reputation: 114792
The attr
binding in KO will actually remove the attribute if the value is false
, null
, or undefined
. So, if you bind against an observable and then set it to one of those values (not empty string), then the attribute will be removed.
Upvotes: 28
Reputation: 9224
You can use conditional binding to show the element
http://knockoutjs.com/documentation/if-binding.html
What you could do, is have two versions of the element, one with the attr binding, and one without, and use the following code to figure out what should be displayed for that particular binding.
<!-- ko if: someExpressionGoesHere -->
<element data-bind="attr: { testattribute: 'a'}></element>
<!-- /ko -->
<!-- ko if: someExpressionGoesHere == false -->
<element></element>
<!-- /ko -->
Upvotes: 0