user1400915
user1400915

Reputation: 1943

Controlling styles in knockoutjs based on the values

I have two divs generated dynamically using knockoutbinding as follows:

<div id="simdivs" data-bind="text:$data.Name,visible:$data.Name !== undefined"></div>

I want to conditionally give a css styling as "margin-left:1px" if the data.Name from the second div is "" (empty) else "margin-left:0px" how do I achieve this using knockout js?

Upvotes: 1

Views: 191

Answers (1)

Jamie Eltringham
Jamie Eltringham

Reputation: 806

Use the Style data-bind

<div id="simdivs" data-bind="style: {marginLeft: $data.Name === null ? '1px' : '0px'}></div>

Do note that it is written as marginLeft instead of margin-left

Upvotes: 1

Related Questions