PDA

View Full Version : Javascript loop elements



lindm
12-01-2007, 08:06 AM
Trying to accomplish this:

I have a form with many form elements. I want the value of the input fields that end with CY be transferred to the input fields that end with PY. So form input with name xxxCY will be moved to form input xxxPY, form input with name yyyCY will be moved to form input yyyPY, etc.

Having trouble formulating the javascript.

Example:

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script language="JavaScript">

function movecytopy()
{
for(i=0; i<document.FormName.elements.length; i++)
{
NEED HELP WITH THIS PART
}
}
</script>

</head>

<body>
<form id="form1" name="form1" method="post" action="">
<table width="100" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input type="button" name="button" id="button" value="MOVE CY TO PY" onclick="movecytopy()" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><input name="example1CY" type="text" id="example1CY" size="15" /></td>
<td><input name="example1PY" type="text" id="example1PY" size="15" /></td>
</tr>
<tr>
<td><input name="example2CY" type="text" id="example2CY" size="15" /></td>
<td><input name="example2PY" type="text" id="example2PY" size="15" /></td>
</tr>
<tr>
<td><input name="example3CY" type="text" id="example3CY" size="15" /></td>
<td><input name="example3PY" type="text" id="example3PY" size="15" /></td>
</tr>
<tr>
<td><input name="example4CY" type="text" id="example4CY" size="15" /></td>
<td><input name="example4PY" type="text" id="example4PY" size="15" /></td>
</tr>
</table>
</form>
</body>
</html>

jscheuer1
12-01-2007, 08:54 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MOVE CY TO PY</title>

<script type="text/javascript">
function movecytopy(el){
for (var c, els=el.form.elements, i = els.length-1; i > -1; --i)
if((c=els[i].name)&&/CY$/.test(c))
for (var p, j = els.length-1; j > -1; --j)
if((p=els[j].name)&&c.replace(/CY$/, 'PY')==p)
els[j].value=els[i].value, els[i].value='';
}
</script>

</head>

<body>
<form id="form1" name="form1" method="post" action="">
<table width="100" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input type="button" name="button" id="button" value="MOVE CY TO PY" onclick="movecytopy(this)"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><input name="example1CY" type="text" id="example1CY" size="15"></td>
<td><input name="example1PY" type="text" id="example1PY" size="15"></td>
</tr>
<tr>
<td><input name="example2CY" type="text" id="example2CY" size="15"></td>
<td><input name="example2PY" type="text" id="example2PY" size="15"></td>
</tr>
<tr>
<td><input name="example3CY" type="text" id="example3CY" size="15"></td>
<td><input name="example3PY" type="text" id="example3PY" size="15"></td>
</tr>
<tr>
<td><input name="example4CY" type="text" id="example4CY" size="15"></td>
<td><input name="example4PY" type="text" id="example4PY" size="15"></td>
</tr>
</table>
</form>
</body>
</html>

lindm
12-01-2007, 11:57 AM
Works in my initial testing. Wonderful!