jokulmorder jokulmorder - 8 months ago 48
CSS Question

Applying a % style binding with knockout via function not working

I want to set the width of a div equal to a percentage calculated by a javascript method. I can get knockout to apply the style binding properly using this:

<div class="bar" data-bind="style: { width: '50%'}"></div>

but when I try to use a function to generate the output, it breaks:

<div class="bar" data-bind="style: { width: function(){return '50' + '%';}}"></div>


It turns out you can get it to work with an anonymous function, you just need to explicitly call that function:

<div data-bind="style: { width: function(){ return '50%'; }() }"></div>