Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8818431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:06:13+00:00 2026-06-14T05:06:13+00:00

I think this has got to be a common situation and I’m wondering if

  • 0

I think this has got to be a common situation and I’m wondering if there’s an accepted convention of how to handle this in Knockout. You have a “yes-no” dropdown (or pair of radio buttons), and it’s default value is a blank item (or both unchecked, for the radio buttons). The user must make a choice in order to continue.

This doesn’t perfectly map to a boolean value in your model, because there are actually three possible values. True, false, and no user selection. In C# you might consider using a nullable boolean, and in Java you might use java.lang.Boolean. In both cases, “null” could represent no user selection.

JavaScript doesn’t have nullables, but since it does not enforce variable types, you could adopt a convention that a particular variable can be null, true, or false, and use it in a similar fashion to a nullable boolean in C# or a java.lang.Boolean.

First off, there’s the issue of binding to boolean. Knockout wants all bound values to be string types by default. This is discussed here and here, and the solution provided by RP Niemeyer is to use a custom binding, like this: (Link to JS Fiddle for this example)

ko.bindingHandlers.booleanValue = {
    init: function(element, valueAccessor, allBindingsAccessor) {
        var observable = valueAccessor(),
            interceptor = ko.computed({
                read: function() {
                    return observable().toString();
                },
                write: function(newValue) {
                    observable(newValue === "true");
                }                   
            });

        ko.applyBindingsToNode(element, { value: interceptor });
    }
};

So I used this as my starting point, and I came up with this custom binding. It seems to work. I’m interested in community feedback on this approach. Checkout the jsfiddle for this to experiment with it yourself. Any drawbacks and/or scalability issues with this?

ko.bindingHandlers.nullableBooleanValue = {
    init: function(element, valueAccessor, allBindingsAccessor) {
        var observable = valueAccessor(),
            interceptor = ko.computed({
                read: function() {                                           
                    console.log(observable());
                    console.log(typeof(observable()));

                    var result = null;
                    if(observable() === true){
                        result = "true";
                    } else if(observable() === false){
                        result = "false";
                    } else { // Default is null, which represents no user selection
                        result = "null";
                    }

                    console.log("transforming on read:")
                    console.log(typeof(observable()));
                    console.log(observable());
                    console.log(typeof(result));
                    console.log(result);
                    return result;
                },
                write: function(newValue) {
                    var result = null;
                    if(newValue === "true"){
                        result = true;
                    } else if(newValue === "false"){
                        result = false;
                    } else { // Default is null, which represents no user selection
                        result = null;
                    }

                    console.log("transforming on write:")
                    console.log(typeof(newValue));
                    console.log(newValue);
                    console.log(typeof(result));
                    console.log(result);
                    observable(result);
                }                   
            });

        ko.applyBindingsToNode(element, { value: interceptor });
    }
};

var model = {
    state: ko.observable(null)
};

ko.applyBindings(model);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:06:14+00:00Added an answer on June 14, 2026 at 5:06 am

    Ok, the extender method just did not work the way I wanted it do, so I dropped it (still in the edit history, if you’re curious). I modified your binding to have it place the options on, so that you aren’t specifying them in the HTML. You also have the option to specify the “Null” option text (you could expand this to allow setting each label).

    This method lets you treat the observable like a standard nullable boolean. Here is the HTML (note, the nullLabel is completely optional):

    <select data-bind="yesNoNull: answer, nullLabel: 'Null' "></select>
    <pre data-bind="text: ko.toJSON($root, null, 2)"></pre>​
    

    Here is the binding:

    ko.bindingHandlers.yesNoNull = {    
        init: function(element, valueAccessor, allBindingsAccessor) {
            var target = valueAccessor();
            var nullLabel = allBindingsAccessor().nullLabel || "";
            var options = function() { return [ nullLabel, "Yes", "No"]; };
            ko.bindingHandlers.options.update(element, options, allBindingsAccessor);
    
            var observable = valueAccessor(),
                interceptor = ko.computed({
                    read: function() {
                        var result = nullLabel;
                        if(observable() === true){
                            result = "Yes";
                        } else if(observable() === false){
                            result = "No";
                        }
                        return result;
                    },
                    write: function(newValue) {
                        var result = null;
                        if(newValue === "Yes"){
                            result = true;
                        } else if(newValue === "No"){
                            result = false;
                        }
                        observable(result);
                    }                   
                });
    
            ko.applyBindingsToNode(element, { value: interceptor });
        }
    };
    

    And here is the fiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this question is quite common. Recently I have got a requirement that
This has been driving me crazy. I think I have everything in place but
There are similar questions to this, but I don't think anyone has asked this
How would I do this? I think this has something to do with editing
I think this question has been asked many a times but I've been searching
I don't think this question has been asked before. I'm a bit confused on
I know this topic has been discussed but I think it has some differences.
I'm hoping this question has a very simple answer. I can think of ways
I think this could be a very easy question for you. But I have
The answer to this has got to be simple, but I sure don't see

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.