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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:06:39+00:00 2026-05-19T04:06:39+00:00

I have a javascript object with several properties. The object also contains an instance

  • 0

I have a javascript object with several properties. The object also contains an instance method that returns the result of a calculation on two of the object’s properties.

I’m using the new Jquery .link() plugin (http://api.jquery.com/link/), to update the UI and form elements with the object property values and vice versa, whenever either the form or the object is updated.

This is all working except for one of the form elements, which should contain the result of the object’s instance method but never gets updated.

I’ve put a button on the form to manually check the value of Discrepancy, and this works but the jquery link plugin is not populating the input elements automatically, although it is populating the input elements linked to the object properties. All elements have an id and name attributes.

Can anyone tell me where I’m going wrong?

My javascript:

<script>

   function Product() {
       this.InStock = 0;
       this.Ordered = 0;

   }

   // object's instance method
   Product.prototype.Discrepancy = ComputeDiscrepancy;

   function ComputeDiscrepancy() {
       return this.InStock - this.Ordered;
   }

   $(document).ready(function () {

       var products = new Array();

       products[0] = new Product();
       products[1] = new Product();

       $("form").link(products[0], {
           InStock: "product0_InStock",
           Ordered: "product0_Ordered",
           Discrepancy: "product0_Discrepancy"
       });

       $("form").link(products[1], {
           InStock: "product1_InStock",
           Ordered: "product1_Ordered",
           Discrepancy: "product1_Discrepancy"
       });
       // button for me to manually check discrepancy method works
       $("#checkData").click(function () {
           alert(products[0].InStock);
           $("#product0_Discrepancy").val(products[0].Discrepancy());
       });

   });   </script>

HTML:

<table>
    <tr>
        <th></th><th>Product 1</th><th>Product 2</th>
    </tr>
    <tr>
        <td> In Stock</td>
        <td><input id="product0_InStock" name="product0_InStock"/></td>
        <td><input id="product1_InStock" name="product1_InStock"/></td>

    </tr>

    <tr>
        <td>Ordered</td>
        <td><input id="product0_Ordered" name="product0_Ordered"/></td>
        <td><input id="product1_Ordered" name="product1_Ordered"/></td>
    </tr>

    <tr>
        <td>Discrepancy</td>
        <td><input id="product0_Discrepancy" name="product0_Discrepancy"/></td>
        <td><input id="product1_Discrepancy" name="product1_Discrepancy"/></td>
    </tr>

</table>
  • 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-05-19T04:06:39+00:00Added an answer on May 19, 2026 at 4:06 am

    The problem is that Discrepancy is a method of a Product object and not a property. You can’t link a DOM element to a method, only to a property.

    The way I solved the problem is to (a) make Discrepancy a property, and (b) use the convert and convertBack methods in order to ensure that this new property was updated properly.

    Here’s my script block:

    function Product() {
        this.InStock = 0;
        this.Ordered = 0;
        this.Discrepancy = 0;
    }
    
    var products = [];
    
    $(document).ready(function () {
    
        products[0] = new Product();
        products[1] = new Product();
    
        var calcDiscrepancy = function(value, source, target) {
            $(target).data("Discrepancy", target.InStock - target.Ordered);
            return value;
        };
    
        var changeInStock = function (value, source, target) {
            target.InStock = value;
            return calcDiscrepancy(value, source, target)            ;
        };
    
        var changeOrdered = function (value, source, target) {
            target.Ordered = value;
            return calcDiscrepancy(value, source, target)            ;
        };
    
        var showDiscrepancy = function(value, source, target) {
            $(target).text(value.toString());
        };
    
        $("form").link(products[0], {
            InStock: {
                name: "product0_InStock",
                convert: changeInStock
            }, 
            Ordered: {
                name: "product0_Ordered",
                convert: changeOrdered
            },
            Discrepancy: {
                name: "product0_Discrepancy",
                convertBack: showDiscrepancy
            }
        });
    
        $("form").link(products[1], {
            InStock: {
                name: "product1_InStock",
                convert: changeInStock
            }, 
            Ordered: {
                name: "product1_Ordered",
                convert: changeOrdered
            },
            Discrepancy: {
                name: "product1_Discrepancy",
                convertBack: showDiscrepancy
            }
       });
    });
    

    Going through it:

    1. add the Discrepancy property to the Product class, with default 0. Throw away the ComputeDiscrepancy stuff.

    2. write a couple of functions that take note of changes in InStock and Ordered. These will update the relevant properties of the target object and then call a function called calcDiscrepancy and return its return value.

    3. calcDiscrepancy will update the value to the target object’s Discrepancy property. To ensure that all the events are triggered with this change, I used jQuery’s .data() method.

    4. write a showDiscrepancy method that would update the target’s contents with the value passed in (the target is going to be the DOM element). I also changed the “discrepancy” elements to rather than text boxes: it didn’t make sense otherwise.

    5. The calls to link() now map the relevant DOM elements to the relevant properties and specify converters for each. For the InStock property the converter is a convert type and calls changeInStock, etc. For the Discrepancy property, the converter is a convertBack type that calls showDiscrepancy. (In essence convert goes from the DOM element to the object property, and convertBack goes from the property to the element.)

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

Sidebar

Related Questions

I have a simple javascript object with several unknown properties containing a value. The
Let's say I have a JavaScript object that is made up of several key/value
I have a numeric javascript array, that contains several objects with geodata in it.
I have a Javascript object that requires 2 calls out to an external server
I have a Javascript object that basically represents a Row in an .NET GridView.
I have a JavaScript object with thousands of properties and what i want if
I have a piece of jQuery that requests a file (using .load() method) with
I'm using javascript and jQuery. I have a problem that my web app has
I have some constants in JavaScript that I'd like to reuse in several files
I have a JavaScript object called ShippingUI: function ShippingUI(){ ... } It has several

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.