<!-- JavaScript:DOM code snippet 7. -->
<!-- Reference and gain access to nodes or elements with dot and array notation:
using an array to store form fields' values and a 'for loop' to access the values. -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- Code source: http://www.justfigures.co.uk/ -->
<!-- A resource for web developers using XHTML, CSS, JavaScript, PHP -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View code snippet page</title>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
/* accessing form elements, controls or fields */
function retrieveValues()
{
/* declare a variable and assign to the first form */
var e = document.forms[0]
/* declare an array of undetermined size to store the values */
var valueArray = new Array()
/* loop through the form fields assigning the values to the array */
for(var i=0; i<e.elements.length; i=i+1)
{
valueArray[i] = e.elements[i].value
}
/* loop through the array to access the stored values */
for (var j=0; j<valueArray.length; j=j+1)
{
window.alert("The values in this form are: "+valueArray[j])
}
}
//]]>
</script>
</head>
<body>
<!-- this is the first form in the document -->
<form name="frmOrder" id="frmOrder">
<p>Product:</p><input type="text" name="txtProduct" size="50" value="Potatoes" /><br />
<p>Order size:</p><input type="text" name="txtOrder" size="50" value="500" /><br />
<p>Cost:</p><input type="text" name="txtCost" size="50" value="100" /><br />
</form>
<br />
<!-- this is the second form in the document -->
<form name="retrieveForm" id="retrieveForm">
<!-- calls function when button is clicked -->
<input type="button" value="Access this form's field values" onclick="retrieveValues()" />
</form>
</body>
</html>