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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:21:34+00:00 2026-06-18T16:21:34+00:00

I’m trying to create a list of check boxes with Knockout.js. The list itself

  • 0

I’m trying to create a list of check boxes with Knockout.js. The list itself comes from a data-bound source, as well as which items in the list are selected. The model I have looks something like this:

var ViewModel = function ()
{
   this.areas = [{AreaId: 1, Name: 'Test 1'}, {AreaId: 2, Name: 'Test 2'}, {AreaId: 3, Name: 'Test 3'}];
   this.AreasImpacted = ko.observableArray([1, 2]);
};

Now, I want to create a list of check boxes with the labels Test 1, Test 2, and Test 3. I want to have Test 1 and Test 2 checked. My HTML looks like so:

<span class="areas" data-bind="foreach: areas">
  <label><input type="checkbox" data-bind="value: AreaId, checked: $parent.AreasImpacted" /><span data-bind="text: Name"></span></label>
</span>

This does draw each check box with the proper name, and I can verify the value attribute of each checkbox is being set correctly, however nothing gets checked! I’ve also tried setting this.AreasImpacted to just 2. When I do that, all 3 checkboxes are checked!

Totally confused!

Update:

If I change the model to:

this.AreasImpacted = ko.observableArray(['1', '2']);

Then things work as expected.

If I had to guess, I’d say the for-each binding is translating each value to a string. I’m curious if this is by-design, or a Knockout.js bug. I would expect the input’s value to be numeric, as that’s what I bound it to.

Bug Filed:

Since the above code does not work as documented, I’ve filed a bug on GitHub.

  • 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-18T16:21:35+00:00Added an answer on June 18, 2026 at 4:21 pm

    You’ll need to change things up a bit, to check if the AreaId of the area you’re rendering is contained in the AreasImpacted array:

    data-bind="value: AreaId, checked: $parent.AreasImpacted.indexOf(AreaId) >= 0"
    

    See this fiddle for a working demo.

    NOTE: Using this technique, checking or unchecking one of the bound check boxes will not update the data source.

    Alternatively, you can make the array a list of strings, like such:

    this.AreasImpacted = ko.observableArray(['1', '2']);
    

    See this fiddle for a demo.


    As noted in the comments, the KnockoutJS documentation seems to suggest that the OP’s original syntax should work. You can even get that to work by making the items in the array into strings (demo).

    The KnockoutJS source can tell us why this happens. Look at the checked.js default binding to see the relevant bit of code that will decide whether to check an input or not:

    element.checked = ko.utils.arrayIndexOf(value, element.value) >= 0;
    

    In this context:

    • value is the array of area id’s (numbers, in the OP’s original code)
    • element is the <input .../> checkbox HtmlElement

    As such, the element.value will always be of type string, which is why the checkbox will only be checked if the items in the array are also strings.

    Whether this is a bug in KnockoutJS, or just some unexpected but logical behavior: I’m not sure.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.