Results 1 to 4 of 4
  1. #1

    form variable names which are arrays and javascript question

    I have a few text fields that that are named as arrays, ie.

    <input type=text name="Cost[1]">
    <input type=text name="Cost[2]">
    <input type=text name="Cost[3]">
    <input type=text name="Cost[4]">

    I am trying to access those form names using javascript, but it's not working.

    Anyone have a clue how to access a form variable name that has a bracket in the name? And before anyone asks, the reason I put my names into an array is for processing by a backend php script.

    My ultimate goal is for the javascript to output the total of the four form fields in another form field. I have the code to total but it only works with variables named like this Cost_1, Cost_2, etc etc.

    Thanks for any help in advance!


  2. #2
    Join Date
    May 2004
    There are a few workarounds.

    One would be to also generate a hidden form element for each field, but with a name that JS can work with.

    Another would be to use variable variables with PHP with say name="Cost1"

    I think that a possible hack would be to use id="Cost1" along with name="Cost[1]", then use JS's getElementById and normal PHP.
    Not sure if this is a good idea, actually, so I would advise against it.
    char*s="#include<cstdio>%cchar*s=%c%s%c;%cint main(){std::printf(s,10,34,s,34,10);}";
    int main(){std::printf(s,10,34,s,34,10);}

  3. #3
    I thought about that, but that won't work becuase the form I'm actually dealing with has about 100 cost fields and adding a hidden field for each field would be a nightmare.

  4. #4
    I solved my own problem, here is what I did for anyone else that run's in to this problem.

    var txtString = document.form.whatever["Cost[Ticket1][0]"].value;

    and to convert to a float, just use parseFloat ie


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts