trouble using correct syntax for getelementbyId()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg

    trouble using correct syntax for getelementbyId()

    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(row Number) {
    document.getEle mentsById('matP erPiece' + rowNumber).valu e =
    (document.getEl ementsById('mat POTotal' + rowNumber) /
    document.getEle mentsById('Quan tityMade'));
    }

    I'm not sure how to access the values in the object that is returned
    by the following syntax:
    var newObj = document.getEle mentsById('fiel dname');

    Thanks for your help I appreciate any suggestions you can provide.

    Greg.
  • Richard Cornford

    #2
    Re: trouble using correct syntax for getelementbyId( )

    "Greg" <gregmckone@yah oo.com> wrote in message
    news:3816e6d1.0 308171613.62fac d2d@posting.goo gle.com...[color=blue]
    >I have the following code (a one line function)[/color]
    [color=blue]
    >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(row Number) {
    > document.getEle mentsById('matP erPiece' + rowNumber).valu e =
    >(document.getE lementsById('ma tPOTotal' + rowNumber) /
    >document.getEl ementsById('Qua ntityMade'));
    >}
    >
    >I'm not sure how to access the values in the object that
    >is returned by the following syntax:
    >var newObj = document.getEle mentsById('fiel dname');
    >
    >Thanks for your help I appreciate any suggestions you can provide.[/color]

    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'+r owNumber]

    - 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(row Number){
    var els = document.forms['costsheet'].elements;
    els['matPerPiece'+r owNumber].value =
    (els['matPOTotal'+ro wNumber].value /

    els['QuantityMade'+ rowNumber].value);
    }

    Richard.


    Comment

    Working...