Field looping

U

UKuser

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
 
T

Thomas 'PointedEars' Lahn

UKuser said:
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
 
T

Thomas 'PointedEars' Lahn

UKuser said:
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
 
E

Evertjan.

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>
========================================
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top