Hi,
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>