Getting form values with looped name insertions from the DOM?

Discussion in 'Javascript' started by Brian A, Oct 14, 2007.

  1. Brian A

    Brian A Guest

    Let's say I have, in a form, the name 'firstName' .
    eg. <input type="text" name="firstName" />
    When the form is submitted I can get a value for this with syntax:-
    fn=form.firstName.value;
    This works. 'fn' gives the value of 'firstName', so no problem there.

    However, it means that for every name in the form I have
    to include a similar statement. I want to put this in a loop and feed
    in different names. I can get them from the DOM.

    Clearly it obviously isn't possible to do the following, though it
    illustrates what I want to do.
    x=firstName;
    fn=form.x.value;
    (where x replaces 'firstName')
    So what method can I use?








    ---
    Remove 'no_spam_' from email address.

    Sign the petition to get High Definition TV via Freeview.
    Get your friends to sign too!
    Ofcom want to auction off the spectrum needed for Hi Def.
    TV.
    http://petitions.pm.gov.uk/High-Definition/
    ---
    Brian A, Oct 14, 2007
    #1
    1. Advertising

  2. Brian A

    Brian A Guest

    On Sun, 14 Oct 2007 19:39:30 -0400, Randy Webb
    <> wrote:

    >Brian A said the following on 10/14/2007 5:34 PM:
    >> Let's say I have, in a form, the name 'firstName' .
    >> eg. <input type="text" name="firstName" />

    >
    >Why do people keep trying to use XHTML on the web?
    >
    >> When the form is submitted I can get a value for this with syntax:-
    >> fn=form.firstName.value;
    >> This works. 'fn' gives the value of 'firstName', so no problem there.

    >
    >"When the form is submitted"? Are you working on the server or the client?

    Client.
    >
    >> However, it means that for every name in the form I have
    >> to include a similar statement. I want to put this in a loop and feed
    >> in different names. I can get them from the DOM.
    >>
    >> Clearly it obviously isn't possible to do the following, though it
    >> illustrates what I want to do.
    >> x=firstName;
    >> fn=form.x.value;
    >> (where x replaces 'firstName')
    >> So what method can I use?

    >
    >Assuming you are referring to client side, you use the elements collection:
    >
    >form.elements[x].value
    >

    Thank you!!
    That solves my problem.

    ---
    Remove 'no_spam_' from email address.

    Sign the petition to get High Definition TV via Freeview.
    Get your friends to sign too!
    Ofcom want to auction off the spectrum needed for Hi Def.
    TV.
    http://petitions.pm.gov.uk/High-Definition/
    ---
    Brian A, Oct 15, 2007
    #2
    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. Dmitri Shvetsov

    Inheritanse, insertions and not only that

    Dmitri Shvetsov, Apr 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    353
    Michael Mayer [C# MVP]
    Apr 10, 2004
  2. Marcio DeBarros
    Replies:
    0
    Views:
    321
    Marcio DeBarros
    Oct 9, 2003
  3. Replies:
    7
    Views:
    391
  4. David
    Replies:
    2
    Views:
    201
    Bob Barrows [MVP]
    Sep 20, 2007
  5. m4dc4p
    Replies:
    2
    Views:
    87
    Robert Klemme
    Nov 24, 2005
Loading...

Share This Page