I need to disable the anchor tag inside a foreach loop of knockout.js in HTML.
Here is my code:
<a id="aQStreamSkype" data-bind="attr:{href: ''}, click: $parent.StoreUserClick,disable: ($data.SkypeId == 'null')">Skype </a>
I need to disable the anchor tag inside a foreach loop of knockout.js in HTML.
Here is my code:
<a id="aQStreamSkype" data-bind="attr:{href: ''}, click: $parent.StoreUserClick,disable: ($data.SkypeId == 'null')">Skype </a>
With some override magic you can get this behaviour without that your view or ViewModel code need changes
When you include that code the enable binding will work for anhors
Fiddle, it uses my convention library so ignore that part http://jsfiddle.net/xCfQC/4/
I found ko.plus an excellent library which implements command pattern. The 'action' can not be executed until 'canExecute' condition is true.
Disable only works with form elements, not anchor tags. You could use the visible binding instead, and just hide the link if there is no user id. If you do want to show something even if there isn't a user id, then add a span with the opposite visible test, then one will be shown if there is a user id, and the other if there isn't:
As a side note, if SkypeId is an observable, you will need to call it as one in your comparison check:
Anchor tags cannot be disabled.
The easiest is to use
ko if binding
and then render aspan
instead of theanchor
if the skype id is nullHere is a fiddle
If there is no
href
attribute ona
element but only an action in aclick binding
, then an easy way would be passing expressioncondition && handler
to a click binding.If condition is observable you'll need to add parentheses.
It will be evaluated as
false
if condition isfalse
(so nothing will happen),and will be evaluated as a handler if condition is
true
.Fiddle here
Knockout enable/disable binding do not support anchor tags.
So you have 2 solution to this.
Solution 1
Solution 2
This button displays only when your condition is success and it has click binding
This button displays only when your negative condition is success and it do not have click binding