How to you dynamically address field from html page

Discussion in 'Javascript' started by Jon Knutson, Jan 23, 2004.

  1. Jon Knutson

    Jon Knutson Guest

    I have a variable number of lines, with each line being a transaction,
    displayed in my jsp. Each line has an amount value with the name
    equal to the line it is displayed on. For example name=amount0 on
    line 1
    name=amount1 on line 2, etc...

    I am attempting to edit the values in the amount field prior to
    submitting the form. I have a for loop that goes through the lines
    but haven't been able to come up with a way to dynamically address the
    field. In the example below I was hoping that 'i' would append itself
    to the field name 'tranLimit'. Unfortunatly, it doesn't. How would I
    make the field name variable?




    isMoney = /^\d+\.\d{2}$/;

    // See list of all accounts available to user
    function submit_form() {
    continueSubmit = "Y";
    rowCount = document.chglimits.rowCount.value;
    for(i=0;i<rowCount;i++) {
    if(!isMoney.test(document.chglimits."tranLimit"i.value)) {
    alert('Invalid Dollar Amount');
    continueSubmit = "N";
    } else {
    }
    }
    if(continueSubmit == "Y") {
    document.chglimits.cmd.value = 'chg_tranlimit';
    document.forms['chglimits'].submit();
    }
    }
     
    Jon Knutson, Jan 23, 2004
    #1
    1. Advertising

  2. On 23 Jan 2004 12:13:53 -0800, Jon Knutson <> wrote:

    > I have a variable number of lines, with each line being a transaction,
    > displayed in my jsp. Each line has an amount value with the name
    > equal to the line it is displayed on. For example name=amount0 on
    > line 1
    > name=amount1 on line 2, etc...
    >
    > I am attempting to edit the values in the amount field prior to
    > submitting the form. I have a for loop that goes through the lines
    > but haven't been able to come up with a way to dynamically address the
    > field. In the example below I was hoping that 'i' would append itself
    > to the field name 'tranLimit'. Unfortunatly, it doesn't. How would I
    > make the field name variable?


    If you used the collection syntax for accessing forms and their controls,
    you can build up names with the concatenation operator. Your attempt would
    be written:

    document.forms['chglimits'].elements['tranLimit' + i].value

    Mike

    --
    Michael Winter
    d (replace ".invalid" with ".uk" to reply)
     
    Michael Winter, Jan 23, 2004
    #2
    1. Advertising

  3. Jon Knutson

    kaeli Guest

    In article <>,
    enlightened us with...
    >
    > I am attempting to edit the values in the amount field prior to
    > submitting the form. I have a for loop that goes through the lines
    > but haven't been able to come up with a way to dynamically address the
    > field. In the example below I was hoping that 'i' would append itself
    > to the field name 'tranLimit'.


    So close...
    But close only counts with grenades and horseshoes.

    > for(i=0;i<rowCount;i++) {
    > if(!isMoney.test(document.chglimits."tranLimit"i.value))


    Now come on - you can't just stick a variable name after quotes in JSPs,
    either. :)

    document.chglimits.elements["tranLimit"+i].value

    Note: the above is not good cross-browser syntax.
    If supporting non-IE browsers, use
    document.forms["chglimits"].elements["tranLimit"+i].value

    --
    --
    ~kaeli~
    Is it possible to be totally partial?
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
     
    kaeli, Jan 23, 2004
    #3
  4. JRS: In article <>, seen
    in news:comp.lang.javascript, Jon Knutson <>
    posted at Fri, 23 Jan 2004 12:13:53 :-

    > I have a for loop that goes through the lines
    >but haven't been able to come up with a way to dynamically address the
    >field.


    FAQ, 4.39. Read its FAQ before posting to a newsgroup.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Jan 24, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Dixson
    Replies:
    1
    Views:
    522
    Joe Fallon
    May 18, 2004
  2. Vincent De Groote

    structure address = structure first field address ?

    Vincent De Groote, Apr 29, 2009, in forum: C Programming
    Replies:
    31
    Views:
    2,066
  3. Jake Barnes
    Replies:
    15
    Views:
    185
    Thomas 'PointedEars' Lahn
    Feb 7, 2006
  4. Sound
    Replies:
    2
    Views:
    465
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    450
Loading...

Share This Page