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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:38:46+00:00 2026-06-04T20:38:46+00:00

I have run into a strange issue when using Knockout to represent a parent/child

  • 0

I have run into a strange issue when using Knockout to represent a parent/child relationship in my view model as a radio button selection in my view.

In the example below, I have a view model that represents two Applications. Each Application contains three Roles and a Selected Role Id.

In the view, I display the Roles under each Application as a list of radio buttons. The button whose value matches the parent’s Selected Role Id should be checked.

<script src='knockout-2.0.0.js' type='text/javascript'></script>
<script src='knockout.mapping.js' type='text/javascript'></script>

<ul data-bind='template: { name: "applicationTemplate", foreach: Applications }'></ul>

<script type='text/html' id='applicationTemplate'>
  <li><span data-bind='text: Name'></li>
  <ul data-bind='template: { name: "roleTemplate", foreach: Roles }'></ul>      
</script>

<script type='text/html' id='roleTemplate'>
  <li>
    <input type='radio' data-bind='value: Id, 
      checked: $parent.SelectedRoleId(), 
      attr: { name: "roleFor" + $parent.Id(), id: "roleFor" + $parent.Id() + "_" + Id() }' />
    <label data-bind='text: Name, 
      attr: { for: "roleFor" + $parent.Id() + "_" + Id() }'></label>
  </li>        
</script>  

<script type='text/javascript' language='javascript'>

  var data = {
    'Applications': [
      { 'Id': 8, 'Name': 'Weather Predictor', 'SelectedRoleId': 1, 'Roles': [
          { 'Id': 1, 'Name': 'Meteorologist' },
          { 'Id': 2, 'Name': 'Farmer' },
          { 'Id': 3, 'Name': 'Pair of Dice' }               
        ]
      },
      { 'Id': 9, 'Name': 'Daily Crime Report', 'SelectedRoleId': 6, 'Roles': [
          { 'Id': 4, 'Name': 'Superhero' },
          { 'Id': 5, 'Name': 'Commissioner' },
          { 'Id': 6, 'Name': 'Journalist' }
        ]
      }
    ]       
  };

  var viewModel = ko.mapping.fromJS(data);

  ko.applyBindings(viewModel);

</script>

Everything renders fine at first:

  • Weather Predictor
    • [X] Meteorologist
    • [ ] Farmer
    • [ ] Pair of Dice
  • Daily Crime Report
    • [ ] Superhero
    • [ ] Commissioner
    • [X] Journalist

But when I click on “Farmer”, all of the Roles under “Weather Predictor” appear unchecked:

  • Weather Predictor
    • [ ] Meteorologist
    • [ ] Farmer
    • [ ] Pair of Dice
  • Daily Crime Report
    • [ ] Superhero
    • [ ] Commissioner
    • [X] Journalist

Clicking “Farmer” a second time will not only check the radio button, but it will function properly from then on. It’s only the first click that acts buggy. The same problem will happen if I click on “Pair of Dice”, as well, or any other unchecked radio button for that matter.

This problem has been perplexing me for a while, and I’m beginning to wonder if I’m using Knockout improperly here.

Any insight into this issue would be greatly appreciated!

  • 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-04T20:38:48+00:00Added an answer on June 4, 2026 at 8:38 pm

    When you are using the checked binding, you would want to avoid using the value binding with it, as it will attach unnecessary event handlers.

    An easy option is to add the value in your attr binding (so you are just setting the value attribute and not adding event handlers) and then put the checked binding after it (so the value is already set before checked runs) like:

    data-bind='attr: { value: Id, name: "roleFor" + $parent.Id(), id: "roleFor" + $parent.Id() + "_" + Id() }, checked: $parent.SelectedRoleId'
    

    Sample: http://jsfiddle.net/rniemeyer/Y478G/

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

Sidebar

Related Questions

I have run into a strange problem on Android , using jQuery mobile and
I am using a WPF ribbon and I have just run into some strange
I've run into a strange Internet Explorer 8 issue. I have the following Javascript:
I'm fairly new to NHibernate and have run into a strange inheritance chaining issue
I seem to have run into a strange issue whereby a string reference is
I have run into a strange problem, i am creating a lot of dynamically
I seemed to have run into a strange problem with the CCParticleSystem and the
I have run into a very strange behaviour I can’t make sense of. I
I have run into a problem w/ my model for databinding in WPF. I
I seem to have run into a strange bug or more likely some setting

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.