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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:30:06+00:00 2026-06-15T01:30:06+00:00

I am trying to use the visible knockout binding and index within a foreach

  • 0

I am trying to use the visible knockout binding and index within a foreach to implement line by line editing. This is done by creating a display and modify div for each line and using visible binding to select between them. The problem is that the observable does not track between the modify and display divs.

I created this fiddle http://jsfiddle.net/rscidmore/YrsCj/57/

To see the problem, click modify on a line, edit the value and click save. You can see that you now have two values for the same observable.

Javascript:
var myViewModel = {
    vals: ko.observableArray([
        {label: 'first',  item: 'one'},
        {label: 'second', item: 'two'},
        {label: 'third',  item: 'three'}
    ]),
    idx: ko.observable(-1)
};

ko.applyBindings(myViewModel);



HTML:
<body class='ui-widget'>
<div class='container'>
   <!-- ko foreach: vals -->
     <div class='label' data-bind="visible: $root.idx() == $index()">
       <span class='lbl' data-bind="text: label"></span>:
       <input type='text' data-bind="value: item">
       <input type='button' value="save" data-bind="click: function()
         { $root.idx(-1);}" />
     </div>
     <div class='label' data-bind="visible: $root.idx() != $index()">
       <span class='lbl' data-bind="text: label"></span>:
       <span data-bind="text: item"></span>
       <input type='button' value="modify" data-bind="click: function()
         { $root.idx($index());}" />
     </div>
   <!-- /ko -->
</div>
</body>
  • 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-15T01:30:08+00:00Added an answer on June 15, 2026 at 1:30 am

    The ko.observableArray only tracks item addition and removal, and it won’t track if one its items have changed.

    Form the documenation:

    Key point: An observableArray tracks which objects are in the array,
    not the state of those objects

    So you need to have observable properties on the items in order to track their changes:

    vals: ko.observableArray([
            {label: 'first',  item: ko.observable('one')}, 
            {label: 'second', item: ko.observable('two')}, 
            {label: 'third',  item: ko.observable('three')}
        ]),
    

    Demo fiddle.

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

Sidebar

Related Questions

I am trying to use powershell to script some tasks within internet explorer. This
I am trying to use div's to display content on my page. This is
I am need paint my image. I'm trying use JQuery in here this link:
I am trying to use the code in this article that lets you know
I am trying to use Knockout in the following scenario: I have jquery UI
I am trying to use a ListBox to display a possibly infinite list of
I'm trying to use the :visible and :first-child pseudo-selectors together in jQuery and it
I'm trying to use this gem to create a honeypot field, but I'm not
I'm trying to use jquery to set the class of all visible elements of
I'm trying to use a back slash in console.log() and within <p></p> but it

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.