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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:11:30+00:00 2026-06-14T22:11:30+00:00

Example Problem: http://jsfiddle.net/whxD3/12/ In knockout I have a select box that I want to

  • 0

Example Problem: http://jsfiddle.net/whxD3/12/

In knockout I have a select box that I want to use optgroups in. This select box can be rebound depending on what “section” you are currently in. I’m finding due to the way knock is doing its binding order the following problems occur:

HTML:

    <div>
        <a href="#" data-bind="click: page1">Section 1</a>
        <a href="#" data-bind="click: page2">Section 2</a>
    </div>
    <hr/>
    <div data-bind="with: activepage">
        <select data-bind="value: selectedItem">
            <optgroup label="Items">
                <!-- ko foreach: items()  -->
                <option data-bind="value: $data, text: $data"></option>
                <!-- /ko -->
            </optgroup>
            <optgroup label="Constants">
                <option value="foo">foo</option>
            </optgroup>
        </select>
        <br/><br/>
        Selected Item:&nbsp;<span data-bind="text: selectedItem"></span>
    </div>

JavaScript:

        function SubPageViewModel(name) {
            this.name = name;
            this.items = ko.observableArray(["one", "two", "three"]);
            this.selectedItem = ko.observable();
        }

        function PageViewModel() {
            this.pages = [new SubPageViewModel("page1"), new SubPageViewModel("page2")];
            this.activepage = ko.observable(this.pages[0]);

            this.page1 = function() { this.activepage(this.pages[0]); }
            this.page2 = function() { this.activepage(this.pages[1]); }
        }

        ko.applyBindings(new PageViewModel());
  • Because I’m using a comment foreach to fill a select box the data binded selected value of the drop down in null initially even though an item is selected

  • When you navigate away from the current section and navigate back the selected value is reset to null.

I believe this is happening because knockout is binding the selected value before the comment foreach fills the selected box so its value is set to null. Then once the select box is filled nothing is triggering an update to update the selected value.

How do I get around this problem?

  • 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-14T22:11:32+00:00Added an answer on June 14, 2026 at 10:11 pm

    You’re right about the binding order causing a problem. There’s a way to force the descendant bindings to happen first, though. You can do this by creating a custom binding that just binds the descendants and add that binding before the value binding.

    Here is the custom binding:

    ko.bindingHandlers.bindContents = {
        init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            ko.applyBindingsToDescendants(bindingContext, element);
            return { controlsDescendantBindings: true };
        }
    };
    

    Here’s how you’ll use it:

    <select data-bind="bindContents: true, value: selectedItem">
    

    Here is your example using this (and with other cleanup): http://jsfiddle.net/mbest/whxD3/14/

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

Sidebar

Related Questions

Example problem: http://jsfiddle.net/6WYXk/ I have some html: <p>% hello world</p> I want to make
I have seen this example: http://jsfiddle.net/UwEe2/306/ that is implemented with scrollleft . Now I
Here is a live example of my problem http://jsfiddle.net/littlesandra88/xksB9/ I would have expected that
This is my example of my website. http://jsfiddle.net/noppanit/dh92t/ I have a problem like this,
This is my example code: http://jsfiddle.net/pT6dB/ I have a bit of a problem. There
You can see the example here: http://jsfiddle.net/8EHED/8/ This is a tricky problem because I
Code Example of my problem: http://jsfiddle.net/x46RQ/ Goal I want the graph to be a
I've created an example of the problem here: http://jsfiddle.net/jXLSW/ Notice that when you hover
Here is the problem: http://jsfiddle.net/STG22/3/ I want that span would not split in two
I've made this example in jsfiddle to demonstrate my problem: http://jsfiddle.net/paulmason411/7E6vG/ See how the

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.