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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:22:04+00:00 2026-06-18T02:22:04+00:00

I have 2 select lists and I want to sync the index, so when

  • 0

I have 2 select lists and I want to sync the index, so when the first has an index of 1 the second will have an index of 1 etc.

This is my html.

<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/2.2.0/knockout-min.js"></script>

<div>
<select id="selLight" data-bind="options: $root.ddlLight, value: ddlLightSelected"></select>
<select id="selAction" data-bind="options: $root.ddlAction, value: ddlActionSelected"></select>
</div>

and the javascript…

var ViewModel = function() {
    var self = this;

    self.ddlLight  = ko.observableArray(["RED", "AMBER", "GREEN"]);
    self.ddlAction = ko.observableArray(["STOP", "READY", "GO"]);
    self.ddlLightSelected  = ko.observable();
    self.ddlActionSelected = ko.observable();

    self.ddlLightSelected.subscribe(function (event) {
        document.getElementById("selAction").selectedIndex =
            self.ddlLight.indexOf(self.ddlLightSelected());
     });

    self.ddlActionSelected.subscribe(function (event) {
        document.getElementById("selLight").selectedIndex =
            self.ddlAction.indexOf(self.ddlActionSelected());
     });    
};

ko.applyBindings(new ViewModel()); 

I have a fiddle with the exact problem…

http://jsfiddle.net/phykell/2vUTw/

EDIT: I was having a few problems with jsfiddle, so here is a jsbin…
http://jsbin.com/ilomer/4/

…and here’s how to recreate the issue:

  1. Run the jsFiddle
  2. Select GREEN from the LIGHTS (ACTIONS will change to GO) 3. Select STOP from the ACTIONS (LIGHTS should change to RED but they don’t)
  • 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-18T02:22:05+00:00Added an answer on June 18, 2026 at 2:22 am

    The problem is this line of code:

    document.getElementById("selAction").selectedIndex = self.ddlLight.indexOf(self.ddlLightSelected());
    

    You’re directly changing the DOM, not allowing Knockout to kick off the observable pattern.

    If you want something to change, always change the ko.observable, not the JavaScript variable or the DOM. Knockout will recognize the change, and therefor change the DOM itself. The solution would be:

    self.ddlLightSelected.subscribe(function (event) {
          var index = self.ddlLight.indexOf(self.ddlLightSelected());
          self.ddlActionSelected(self.ddlAction()[index]); // Update the Observable, not the DOM
    });
    
    self.ddlActionSelected.subscribe(function (event) {
        var index = self.ddlAction.indexOf(self.ddlActionSelected());
        self.ddlLightSelected(self.ddlLight()[index]); // Update the Observable, not the DOM
    }); 
    

    Updated JS Bin.

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

Sidebar

Related Questions

i have this lists, and i want select li element from jquery when the
I have a table with many rows and each row has select lists. Something
I have two multi-select lists in HTML - one of categories, and another of
I have a page that has multiple select lists and when ever one of
I have two simple select lists and want to add an option to swap
I am using Jquery mobile, and I have these two select lists, I want
I have two select lists, and I want the selection for both to go
I have a select/drop down in my HTML page which has to appear about
I have two lists and I want to select items that are not in
I have two identical select lists as KnockoutJS Observable Arrays. I want to be

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.