I have written this code and it executes well but I would like to
instead of using multiple if statements write a loop that will do the
exact same thing. Can anyone help me with writing a loop?
------------ CODE ------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>Untitl ed Document</title>
<script language="JavaS cript">
<!-- Begin
function calculator(user , number){
var a=0;
var b=0;
var box1 = eval("document. chmod.Matching1 ")
var box2 = eval("document. chmod.Matching2 ")
var box3 = eval("document. chmod.Matching3 ")
var box4 = eval("document. chmod.Matching4 ")
var box5 = eval("document. chmod.Matching5 ")
if (box1.checked == true){
a +=(document.chm od.textField1.v alue*1)
}
if (box2.checked == true){
a +=(document.chm od.textField2.v alue*1)
}
if (box3.checked == true){
a +=(document.chm od.textField3.v alue*1)
}
if (box4.checked == true){
a +=(document.chm od.textField4.v alue*1)
}
if (box5.checked == true){
a +=(document.chm od.textField5.v alue*1)
}
else {
if (box1.checked == false){
b +=(document.chm od.textField1.v alue*1)
}
if (box2.checked == false){
b +=(document.chm od.textField2.v alue*1)
}
if (box3.checked == false){
b +=(document.chm od.textField3.v alue*1)
}
if (box4.checked == false){
b +=(document.chm od.textField4.v alue*1)
}
if (box5.checked == false){
b +=(document.chm od.textField5.v alue*1)
}
document.chmod. Matching_Funds. value=a
document.chmod. Other_Funds.val ue=b
}
}
</script>
</head>
<body>
<form name="chmod">
<table cellpadding="5" width="90%" border="1">
<tr>
<td colspan="3"><st rong>Funding Sources</strong></td>
</tr>
<tr>
<th align="left" width="40%"> Funding Source Name </th>
<th align="left" width="30%"> Matching Funds </th>
<th align="left" width="30%"> Fund Amount </th>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Fundi ng Source 1</option>
<option>Unite d Way</option>
<option>Red Cross</option>
</select>
</td>
<td><input type="checkbox" name="Matching1 " value="1">
Yes </td>
<td>$
<input type="text" size="10" name="textField 1"
onBlur="calcula tor('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Fundi ng Source 1</option>
<option>Unite d Way</option>
<option>Red Cross</option>
</select>
</td>
<td><input type="checkbox" name="Matching2 " value="1">
Yes </td>
<td>$
<input type="text" size="10" name="textField 2"
onBlur="calcula tor('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Fundi ng Source 1</option>
<option>Unite d Way</option>
<option>Red Cross</option>
</select>
</td>
<td><input type="checkbox" name="Matching3 " value="1">
Yes </td>
<td>$
<input type="text" size="10" name="textField 3"
onBlur="calcula tor('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Fundi ng Source 1</option>
<option>Unite d Way</option>
<option>Red Cross</option>
</select>
</td>
<td><input type="checkbox" name="Matching4 " value="1">
Yes </td>
<td>$
<input type="text" size="10" name="textField 4"
onBlur="calcula tor('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Fundi ng Source 1</option>
<option>Unite d Way</option>
<option>Red Cross</option>
</select>
</td>
<td><input type="checkbox" name="Matching5 " value="5">
Yes </td>
<td>$ <input type="text" size="10" name="textField 5"
onBlur="calcula tor('Matching', 1)"></td>
</tr>
<tr>
<td colspan="2"><st rong>Matching Funds Total</strong>
</td>
<td>$ <input name="Matching_ Funds" type="text" size="10"
tabindex="1"></td>
</tr>
<tr>
<td colspan="2"><st rong>Other Funds Total</strong>
</td>
<td>$ <input type="text" size="10" name="Other_Fun ds"
tabindex="2"></td>
</tr>
<tr>
<td colspan="3"><a href="">Add Additional Funding
Sources</a><br>
</td>
</tr>
</table>
</form>
</body>
</html>
instead of using multiple if statements write a loop that will do the
exact same thing. Can anyone help me with writing a loop?
------------ CODE ------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>Untitl ed Document</title>
<script language="JavaS cript">
<!-- Begin
function calculator(user , number){
var a=0;
var b=0;
var box1 = eval("document. chmod.Matching1 ")
var box2 = eval("document. chmod.Matching2 ")
var box3 = eval("document. chmod.Matching3 ")
var box4 = eval("document. chmod.Matching4 ")
var box5 = eval("document. chmod.Matching5 ")
if (box1.checked == true){
a +=(document.chm od.textField1.v alue*1)
}
if (box2.checked == true){
a +=(document.chm od.textField2.v alue*1)
}
if (box3.checked == true){
a +=(document.chm od.textField3.v alue*1)
}
if (box4.checked == true){
a +=(document.chm od.textField4.v alue*1)
}
if (box5.checked == true){
a +=(document.chm od.textField5.v alue*1)
}
else {
if (box1.checked == false){
b +=(document.chm od.textField1.v alue*1)
}
if (box2.checked == false){
b +=(document.chm od.textField2.v alue*1)
}
if (box3.checked == false){
b +=(document.chm od.textField3.v alue*1)
}
if (box4.checked == false){
b +=(document.chm od.textField4.v alue*1)
}
if (box5.checked == false){
b +=(document.chm od.textField5.v alue*1)
}
document.chmod. Matching_Funds. value=a
document.chmod. Other_Funds.val ue=b
}
}
</script>
</head>
<body>
<form name="chmod">
<table cellpadding="5" width="90%" border="1">
<tr>
<td colspan="3"><st rong>Funding Sources</strong></td>
</tr>
<tr>
<th align="left" width="40%"> Funding Source Name </th>
<th align="left" width="30%"> Matching Funds </th>
<th align="left" width="30%"> Fund Amount </th>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Fundi ng Source 1</option>
<option>Unite d Way</option>
<option>Red Cross</option>
</select>
</td>
<td><input type="checkbox" name="Matching1 " value="1">
Yes </td>
<td>$
<input type="text" size="10" name="textField 1"
onBlur="calcula tor('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Fundi ng Source 1</option>
<option>Unite d Way</option>
<option>Red Cross</option>
</select>
</td>
<td><input type="checkbox" name="Matching2 " value="1">
Yes </td>
<td>$
<input type="text" size="10" name="textField 2"
onBlur="calcula tor('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Fundi ng Source 1</option>
<option>Unite d Way</option>
<option>Red Cross</option>
</select>
</td>
<td><input type="checkbox" name="Matching3 " value="1">
Yes </td>
<td>$
<input type="text" size="10" name="textField 3"
onBlur="calcula tor('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Fundi ng Source 1</option>
<option>Unite d Way</option>
<option>Red Cross</option>
</select>
</td>
<td><input type="checkbox" name="Matching4 " value="1">
Yes </td>
<td>$
<input type="text" size="10" name="textField 4"
onBlur="calcula tor('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Fundi ng Source 1</option>
<option>Unite d Way</option>
<option>Red Cross</option>
</select>
</td>
<td><input type="checkbox" name="Matching5 " value="5">
Yes </td>
<td>$ <input type="text" size="10" name="textField 5"
onBlur="calcula tor('Matching', 1)"></td>
</tr>
<tr>
<td colspan="2"><st rong>Matching Funds Total</strong>
</td>
<td>$ <input name="Matching_ Funds" type="text" size="10"
tabindex="1"></td>
</tr>
<tr>
<td colspan="2"><st rong>Other Funds Total</strong>
</td>
<td>$ <input type="text" size="10" name="Other_Fun ds"
tabindex="2"></td>
</tr>
<tr>
<td colspan="3"><a href="">Add Additional Funding
Sources</a><br>
</td>
</tr>
</table>
</form>
</body>
</html>
Comment