"Greg" <gr********@yahoo.com> wrote in message
news:38**************************@posting.google.c om...
I have the following code (a one line function)
I'm trying to calculate a value for text field matPerPiece#
where # is a row number I provide. The value is calculated
from text fields matPOTotal# and QuantityMade.
The elements exist in a form named costsheet
function mattotalrow(rowNumber) {
document.getElementsById('matPerPiece' + rowNumber).value =
(document.getElementsById('matPOTotal' + rowNumber) /
document.getElementsById('QuantityMade'));
}
I'm not sure how to access the values in the object that
is returned by the following syntax:
var newObj = document.getElementsById('fieldname');
Thanks for your help I appreciate any suggestions you can provide.
Given a form named 'costsheet' and an element who's name is an integer
concatenated to the end of 'matPerPiece', the code W3C HTML DOM Level 2
standard (and very back-compatible) property accessor:-
document.forms['costsheet'].elements['matPerPiece'+rowNumber]
- would be the best way of construction a global reference to a from
element.
Properties of that element would normally be accessed with dot notation,
adding, say - .value - to the end of the above reference to access the
"value" property of the element.
It is however more efficient to cache references to objects that
represent stages in repeated property accessors, such as a reference to
the form or the form's elements collection. I assume that the function
above is intended to read the value in the second two elements and
assign the result of the calculation to the value property of the first,
so something like:-
function mattotalrow(rowNumber){
var els = document.forms['costsheet'].elements;
els['matPerPiece'+rowNumber].value =
(els['matPOTotal'+rowNumber].value /
els['QuantityMade'+rowNumber].value);
}
Richard.