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 8848019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:19:41+00:00 2026-06-14T12:19:41+00:00

I have the same data structure for checkboxes and radio buttons. When checking the

  • 0

I have the same data structure for checkboxes and radio buttons. When checking the checkboxes, they return correct boolean value (‘chosen’ variable).

However, when I check the radio buttons, ‘chosen’ always changes to the ‘value’ (integer).

Also the radio buttons don’t get “checked” in the beginning, even though ‘chosen’ == true

Javascript:

function attributeValueViewModel(data) {
    var self = this;
    self.id = ko.observable(data.id);
    self.attributeID = ko.observable(data.attributeID);
    self.value = ko.observable(data.value);
    self.chosen = ko.observable(data.chosen);
}

function viewModel() {
    var self = this;
    self.attributeValues1 = ko.observableArray([]);
    self.attributeValues2 = ko.observableArray([]);
    self.addToList = function(data) {
        ko.utils.arrayForEach(data, function(item) {           
            self.attributeValues1.push(new attributeValueViewModel(item));
            self.attributeValues2.push(new attributeValueViewModel(item));
        });
    };
}

var arr = [
    {
        "id": 55,
        "attributeID": 28,
        "value": "Yes",
        "chosen": false,
    },
    {
        "id": 56,
        "attributeID": 28,
        "value": "No",
        "chosen": true,
    },
    {
        "id": 62,
        "attributeID": 28,
        "value": "Maybe",
        "chosen": false,
    }
];

var vm = new viewModel();
ko.applyBindings(vm);
vm.addToList(arr);

HTML

<b>Checkbox:</b>
<div id="test1">
    <span data-bind="foreach: attributeValues1()">
    <input type="checkbox" data-bind="value: id(), checked: chosen, attr: { name: 'test1' }" />
    <span data-bind="text: value()"></span>
    <span data-bind="text: chosen()"></span>
    </span>
</div>
<br />
<b>Radio:</b>
<div id="test2">
    <span data-bind="foreach: attributeValues2()">
    <input type="radio" data-bind="value: id(), checked: chosen, attr: { name: 'test2' }" />
    <span data-bind="text: value()"></span>
    <span data-bind="text: chosen()"></span>
    </span>
</div>​

Here is my fiddle: http://jsfiddle.net/SN7Vn/1/

Can you please explain this behavior and why the radio buttons don’t update boolean (like checkboxes do)?

  • 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-14T12:19:42+00:00Added an answer on June 14, 2026 at 12:19 pm

    Check boxes and Radio buttons behave differently from each other. When a single observable is bound to the checked binding (as opposed to an array) is where they particularly behave differently.

    For a check box, it will always be a boolean value indicating whether it was checked or not. Simple. If an array, the value will be added/removed from the array.

    However for a radio button, the it will be the value assigned to the radio button. For a single observable, it will only be considered checked the value of what it is bound to is equal to the value. In your case, your value is bound to the id so will only respond if the chosen value is equal to the corresponding id. For an array, it will add/remove the value to the array.

    Since nothing else alters the value of the chosen for the corresponding attribute, it remains the same

    I tweaked your example to demonstrate a bit how the value can affect the checked state. Updated fiddle

    It’s all spelled out in the documentation, look at it closer.


    If you want it to work similarly to how it does for checkboxes, it will take a bit of work to get it right. I have this code which feels a bit fragile due to how the bindings work and how their order dependentness can cause problems. You can try this out, I’m just not sure how robust this is.

    For this to work, you’ll need an observable to keep track of the actual changes to what radio button is actually selected. Then you can subscribe to changes on that observable to update the observable of your choice.

    ko.bindingHandlers.radiochecked = {
        init: function (element, valueAccessor, allBindingsAccessor) {
            // the checked binding depends on the element having an initial value
            // we need to allow bindings that potentially set it to be applied first
            setTimeout(function () {
                var options = ko.utils.unwrapObservable(valueAccessor());
                if (options.checked()) {
                    options.selected(element.value);
                }
                options.selected.subscribe(function (newValue) {
                    options.checked(newValue === element.value);
                });
                ko.applyBindingsToNode(element, { checked: options.selected });
            }, 0);
        }
    };
    

    Then bind it to your radio buttons:

    <input type="radio" data-bind="
        value: id,
        radiochecked: { 
            'checked': chosen,
            'selected': $root.attributeValues2.selected
        },
        attr: { name: 'test2' }" />
    

    Demo

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

Sidebar

Related Questions

i have two data tables with the same structure the first one has one
I am manipulating many instances of the same data structure which can have one
I have Preferences data structure where I have string Value field and enum for
I have a data structure that stores amongst others a 24-bit wide value. I
We have a data structure in sitecore that features the same templates at the
I have 2 tables, same structure, same rows, same data but the first has
I have a growing set of Excel spreadsheets none with the same data structure.
I wanted to compare 2 XMLs, which have the same data but the tag
I am wanting to have the EXACT same data across two WordPress sites that
I have 3 Oracle database instances with the same data and scheme that I

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.