trouble using correct syntax for getelementbyId()

G

Greg

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.

Greg.
 
R

Richard Cornford

Greg said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top