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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:54:16+00:00 2026-06-16T01:54:16+00:00

I’m using KnockoutJs 2.2. I have a record which can have zero-to-many category associations.

  • 0

I’m using KnockoutJs 2.2. I have a record which can have zero-to-many category associations. However, I’m finding that KnockoutJs does not actually bind the value to the items inside the record’s categories array.

The array of categories are {id, name} objects. This is derived from a lookup list.

self.categories = ko.observableArray([{id:1,name:'Test1'},{id:2,name:'Test2'}]);

On my view model, I have a record object. One of the fields on this object is categories (observableArray).

categories: ko.observableArray([])

The record object is called “r” on the viewmodel for brevity in the bindings.

<div class="yui3-u-4-5">
    <!-- ko foreach: r.categories-->
    <select data-bind="options: $parent.categories, optionsCaption:'-', 
optionsValue:'id', optionsText:'name', value:$data" />
    <div class="del" data-bind="click: $parent.removeCat" />
    <br />
    <!-- /ko -->
    <button data-bind="click: addCat">Add category</button>
</div>

When the user adds or removes a category, the following code is called:

self.removeCat = function (data) {
    self.r.categories.remove(data);
};

self.addCat = function () {
    self.r.categories.push(ko.observable(null));
} 

Basically, what happens is that when a category is added into the self.r.categories array, and then you select a category from the drop down, the selected item’s value is not bound to the observable inside the array ($data). The array values remain null.

I have re-created the problem here: http://jsfiddle.net/fxXsq/

Maybe I should be doing this an alternative way?

thanks for your help
Kris

  • 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-16T01:54:17+00:00Added an answer on June 16, 2026 at 1:54 am

    You should not be adding actual new empty observables to your observable array like this.

    self.addCat = function () {
        self.r.categories.push(ko.observable(null)); 
    } 
    

    Furthermore, if you want the Id and Name properties of your objects to be observable, you’ll need a model for them

    self.addCat = function () {
        self.r.categories.push(new Category(null, self));
    };
    
    function Category(data, parent) {
       var self = this; 
       self.Id = ko.observable(-1);
       self.Name = ko.observable('');
    
       self.load = function(data) {
           if (data) {
               self.Id(data.Id);
               self.Name(data.Name);
           }
       }
       self.load(data);
    }
    

    This is my general template I apply to all my models to better support deferred binding and flexibility.

    Now you would have an observableArray r that contains Category objects with observable properties.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
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.

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.