Field looping

Discussion in 'Javascript' started by UKuser, Aug 16, 2007.

  1. UKuser

    UKuser Guest

    Hi,

    How do I loop through 5 fields each named field,
    field2,field3,field4,field5 within this statement?

    document.forms[0].field|whatgoeshere?|.value

    For example:
    for (i = 0; i < ct; i++)
    {
    name = escape(document.forms[0].field('1').value);
    alert(name);
    }

    This will fail, but I need to know how I should include a changing
    element after the field name titled i within my looping statement.

    Thanks

    A
     
    UKuser, Aug 16, 2007
    #1
    1. Advertising

  2. UKuser wrote:
    > How do I loop through 5 fields each named field,
    > field2,field3,field4,field5 within this statement?
    >
    > document.forms[0].field|whatgoeshere?|.value
    >
    > For example:
    > for (i = 0; i < ct; i++)
    > {
    > name = escape(document.forms[0].field('1').value);
    > alert(name);
    > }
    >
    > This will fail, but I need to know how I should include a changing
    > element after the field name titled i within my looping statement.


    for (var es = document.forms[0].elements, i = es && es.length; i--;)
    {
    name = escape(es["field" + i].value);
    alert(name);
    }

    You SHOULD NOT encode the field values if you use a `form' element to submit
    them. The proprietary escape() also is deprecated in favor of the standards
    compliant encodeURI() and encodeURIComponent() methods; the former should
    only be used as a fallback where the latter methods, as determined by proper
    feature detection, are not available.


    PointedEars
    --
    "Use any version of Microsoft Frontpage to create your site. (This won't
    prevent people from viewing your source, but no one will want to steal it.)"
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
     
    Thomas 'PointedEars' Lahn, Aug 16, 2007
    #2
    1. Advertising

  3. UKuser wrote:
    > How do I loop through 5 fields each named field,
    > field2,field3,field4,field5 within this statement?
    >
    > document.forms[0].field|whatgoeshere?|.value
    >
    > For example:
    > for (i = 0; i < ct; i++)
    > {
    > name = escape(document.forms[0].field('1').value);
    > alert(name);
    > }
    >
    > This will fail, but I need to know how I should include a changing
    > element after the field name titled i within my looping statement.


    for (var es = document.forms[0].elements, i = es && ct; i--;)
    {
    name = escape(es["field" + i].value);
    window.alert(name);
    }

    You SHOULD NOT encode the field values if you use a `form' element to submit
    them. The proprietary escape() also is deprecated in favor of the standards
    compliant encodeURI() and encodeURIComponent() methods; the former should
    only be used as a fallback where the latter methods, as determined by proper
    feature detection, are not available.


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
     
    Thomas 'PointedEars' Lahn, Aug 16, 2007
    #3
  4. UKuser

    Evertjan. Guest

    UKuser wrote on 16 aug 2007 in comp.lang.javascript:

    > Hi,
    >
    > How do I loop through 5 fields each named field,
    > field2,field3,field4,field5 within this statement?
    >
    > document.forms[0].field|whatgoeshere?|.value
    >
    > For example:
    > for (i = 0; i < ct; i++)
    > {
    > name = escape(document.forms[0].field('1').value);
    > alert(name);
    > }


    ========= test.html ====================
    <form>
    <input name=n1 value=v1>
    <input name=n2 value=v2>
    <input name=nLast value=vLast>
    </form>

    <script type='text/javascript'>

    var name='';
    var fields = document.forms[0].elements;

    for (i = 0; i < fields.length; i++)
    name += fields.name+': '+fields.value+'\n';
    alert(name);

    </script>
    ========================================

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Aug 16, 2007
    #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. Replies:
    4
    Views:
    1,120
  2. WC Justice

    Build SQL string looping through field names

    WC Justice, Jan 24, 2005, in forum: ASP General
    Replies:
    2
    Views:
    187
    Bob Barrows [MVP]
    Jan 24, 2005
  3. Sound
    Replies:
    2
    Views:
    461
    Randy Webb
    Sep 28, 2006
  4. jr
    Replies:
    3
    Views:
    433
  5. Replies:
    5
    Views:
    283
Loading...

Share This Page