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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:07:37+00:00 2026-06-13T14:07:37+00:00

I’m trying to think of the most elegant way to do this using knockout

  • 0

I’m trying to think of the most elegant way to do this using knockout js.

Say I need to sum up 3 values, but the result sum can also be edited…how would you do this using knockout js?

I was thinking along the lines of setting deactivating the values that make up the sum when a user edits the sum value and reactivating them if the sum field is blank.

See this jsfiddle for a starting point: http://jsfiddle.net/faHHB/

A similar example for decomposing a string using a computed observable exists in the knockout documentation:

function MyViewModel() {
    this.firstName = ko.observable('Planet');
    this.lastName = ko.observable('Earth');

    this.fullName = ko.computed({
        read: function () {
            return this.firstName() + " " + this.lastName();
        },
        write: function (value) {
            var lastSpacePos = value.lastIndexOf(" ");
            if (lastSpacePos > 0) { // Ignore values with no space character
                this.firstName(value.substring(0, lastSpacePos)); // Update "firstName"
                this.lastName(value.substring(lastSpacePos + 1)); // Update "lastName"
            }
        },
        owner: this
    });
}

ko.applyBindings(new MyViewModel());

Edit
For more clarity, I’m not sure how best to represent this situation:

Imagine the 3 fields represent value of your house, value of your car, value of your retirement savings and the sum represents total value of your assets.

All fields should be editable, and if a user enters the 3 values the total value should be calculated. The total value can also be directly edited (incase the user has other savings that aren’t listed). I’m not sure of the best way to represent this…

When the user edits the total, options I can think of are:

  1. Divide the total by 3 and populate the other values
  2. Grey out the input values, and only allow users to change them again when the total value is blank.
  3. Have a checkbox labelled calculate, which sums the value when ticked and lets the user edit when unticked.

I was trying to ask what the best way to represent this situation is as well as how to implement it using knockout. Apologies if I wasn’t clear.

I’ve settled on approach 3 for now: jsfiddle.net/faHHB/10

  • 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-13T14:07:38+00:00Added an answer on June 13, 2026 at 2:07 pm

    You probably want a view model that:-

    Holds the sum as a separate observable

    Has a flag to determine whether the sum should be automatically computed

    Calculates the value of editedTotal when the flag is true, otherwise just returns the present value.

    var testVm = function() {
      var self = this;
      self.items = ko.observableArray();
      self.editedTotal = ko.observable(null);
      self.autoCalculate = ko.observable(true);
      self.calculatedTotal = ko.computed(function() {
         if ( self.autoCalculate() ){
             // Perform sum if still calculating
             var total = 0;
    
             for ( var i = 0; i < self.items().length ) {
               total += self.items()[i].Amount();  
             }
    
             self.editedTotal(total);
         }
    
         return self.editedTotal();
      });  
    }
    

    When you detect a direct edit on self.editedTotal, set self.autoCalculate to false. At that point, your VM will not attempt to recalculate, and will just return the tracked value.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have thousands of HTML files to process using Groovy/Java and I need to
I need to clean up various Word 'smart' characters in user input, including but
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.