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

  • Home
  • SEARCH
  • 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 9166937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:16:13+00:00 2026-06-17T15:16:13+00:00

I have a select element that is bound to a list of complex javascript

  • 0

I have a select element that is bound to a list of complex javascript objects using the options binding:

<select data-bind="options: availableCountries, optionsText: 'countryName', value: selectedCountry, optionsCaption: 'Choose...'"></select>

where availableCountries is an observable array of javascript objects. You can see the full example in this fiddle.

Now, this works fine when I select an item in the list. However, if I need to update the value from some other part of the application, I will only be able to do so if I use the exact same Country object as lies in the availableCountries array. This has also been established in this related question, but unfortunately it does not solve my problems.

In the documentation of the options binding, they anticipate this problem, and say that I should use the optionsValue binding:

Typically you’d only want to use optionsValue as a way of ensuring
that KO can correctly retain selection when you update the set of
available options. For example, if you’re repeatedly getting a list of
“car” objects via Ajax calls and want to ensure that the selected car
is preserved, you might need to set optionsValue to “carId” or
whatever unique identifier each “car” object has, otherwise KO won’t
necessarily know which of the previous “car” objects corresponds to
which of the new ones.

But this unfortunately will not work, as it also affects the value binding – if I set optionsValue to 'countryName', selectedCountry will only contain the name of the country and not the entire object.

I’m looking for a way to combine these. I still want value to be the entire object, but I want the option value comparison to use a specific property that I specify. Basically, something in the lines of:

<select data-bind="options: availableCountries, optionsText: 'countryName', optionsComparator: 'countryName', value: selectedCountry, optionsCaption: 'Choose...'"></select>

However, as far as I can see, this involves modifying the value binding. Do I have any other options? Can I solve this in some other way? Extenders? A custom binding handler?

  • 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-17T15:16:14+00:00Added an answer on June 17, 2026 at 3:16 pm

    I’ve made a selected binding that fixes this problem, check my collection of bindings

    Demo, the dropdown is at the end of the page
    http://jsfiddle.net/H8xWY/8/

    https://github.com/AndersMalmgren/Knockout.Bindings

    Code

    ko.bindingHandlers.selected = {
        init: function (element, valueAccessor, allBindingsAccessor) {
            var selected = valueAccessor();
            var items = ko.utils.unwrapObservable(allBindingsAccessor().options);
            var key = allBindingsAccessor().optionsKey ? allBindingsAccessor().optionsKey : allBindingsAccessor().optionsText;
    
            var observable = ko.computed({
                read: function () {
                    var value = ko.utils.unwrapObservable(selected);
                    return ko.utils.arrayFirst(items, function (item) {
                        return value != null ? ko.utils.unwrapObservable(item[key]) == ko.utils.unwrapObservable(value[key]) : false;
                    });
                },
                write: function (value) {
                    writeValueToProperty(selected, allBindingsAccessor, "selected", value);
                }
            });
    
            ko.applyBindingsToNode(element, { value: observable });
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a select element that shows multiple options with the same text: <select
I have a select element. This element is looped over using jquery, any options
I have a select element, that posts (just fine) when changed using $.post. But
I have jQuery to select all font elements that are children of the element
How do i select elements that have my custom data attribute data-validate with jQuery
How to select next n hidden elements that have class foo using jQuery?
I have an HTML select element that allows multiple selection. I am populating the
I have a form with a select element that I need to populate with
I have a select element that is in the DOM from page load. Lets
I have a select element which I'm creating through javascript (DOM) var cell_stock_id =

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.