10

I am trying to create a hidden form field from a boolean value in my viewModel.

    <tbody  data-bind="foreach: MediaFiles">
        <tr>
            <td>
                <input type="hidden" 
                        data-bind="attr: { value: MyBool }" />
            </td>
        </tr>
    </tbody>  

I need the input's value to be either "true" or "false" based on what's in the view model. Other attributes have been omitted for clarity.

What's the best way to accomplish this with knockout's binding functionality?

1 Answer 1

18
data-bind="attr: { value: MyBool ? 'true' : 'false' }"

or if MyBool is an observable:

data-bind="attr: { value: MyBool() ? 'true' : 'false' }"

or you could use a computed observable:

MyBool = ko.computed(function(){

   return this.someValue() ? 'true' : 'false';

}, this);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.