D
david.jebo
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>Untitled Document</title>
<script language="JavaScript">
<!-- 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.chmod.textField1.value*1)
}
if (box2.checked == true){
a +=(document.chmod.textField2.value*1)
}
if (box3.checked == true){
a +=(document.chmod.textField3.value*1)
}
if (box4.checked == true){
a +=(document.chmod.textField4.value*1)
}
if (box5.checked == true){
a +=(document.chmod.textField5.value*1)
}
else {
if (box1.checked == false){
b +=(document.chmod.textField1.value*1)
}
if (box2.checked == false){
b +=(document.chmod.textField2.value*1)
}
if (box3.checked == false){
b +=(document.chmod.textField3.value*1)
}
if (box4.checked == false){
b +=(document.chmod.textField4.value*1)
}
if (box5.checked == false){
b +=(document.chmod.textField5.value*1)
}
document.chmod.Matching_Funds.value=a
document.chmod.Other_Funds.value=b
}
}
</script>
</head>
<body>
<form name="chmod">
<table cellpadding="5" width="90%" border="1">
<tr>
<td colspan="3"><strong>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>Funding Source 1</option>
<option>United 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="textField1"
onBlur="calculator('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Funding Source 1</option>
<option>United 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="textField2"
onBlur="calculator('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Funding Source 1</option>
<option>United 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="textField3"
onBlur="calculator('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Funding Source 1</option>
<option>United 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="textField4"
onBlur="calculator('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Funding Source 1</option>
<option>United 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="textField5"
onBlur="calculator('Matching', 1)"></td>
</tr>
<tr>
<td colspan="2"><strong>Matching Funds Total</strong>
</td>
<td>$ <input name="Matching_Funds" type="text" size="10"
tabindex="1"></td>
</tr>
<tr>
<td colspan="2"><strong>Other Funds Total</strong>
</td>
<td>$ <input type="text" size="10" name="Other_Funds"
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>Untitled Document</title>
<script language="JavaScript">
<!-- 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.chmod.textField1.value*1)
}
if (box2.checked == true){
a +=(document.chmod.textField2.value*1)
}
if (box3.checked == true){
a +=(document.chmod.textField3.value*1)
}
if (box4.checked == true){
a +=(document.chmod.textField4.value*1)
}
if (box5.checked == true){
a +=(document.chmod.textField5.value*1)
}
else {
if (box1.checked == false){
b +=(document.chmod.textField1.value*1)
}
if (box2.checked == false){
b +=(document.chmod.textField2.value*1)
}
if (box3.checked == false){
b +=(document.chmod.textField3.value*1)
}
if (box4.checked == false){
b +=(document.chmod.textField4.value*1)
}
if (box5.checked == false){
b +=(document.chmod.textField5.value*1)
}
document.chmod.Matching_Funds.value=a
document.chmod.Other_Funds.value=b
}
}
</script>
</head>
<body>
<form name="chmod">
<table cellpadding="5" width="90%" border="1">
<tr>
<td colspan="3"><strong>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>Funding Source 1</option>
<option>United 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="textField1"
onBlur="calculator('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Funding Source 1</option>
<option>United 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="textField2"
onBlur="calculator('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Funding Source 1</option>
<option>United 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="textField3"
onBlur="calculator('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Funding Source 1</option>
<option>United 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="textField4"
onBlur="calculator('Matching', 1)"></td>
</tr>
<tr>
<td><select>
<option selected>Select Funding Source</option>
<option>Funding Source 1</option>
<option>United 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="textField5"
onBlur="calculator('Matching', 1)"></td>
</tr>
<tr>
<td colspan="2"><strong>Matching Funds Total</strong>
</td>
<td>$ <input name="Matching_Funds" type="text" size="10"
tabindex="1"></td>
</tr>
<tr>
<td colspan="2"><strong>Other Funds Total</strong>
</td>
<td>$ <input type="text" size="10" name="Other_Funds"
tabindex="2"></td>
</tr>
<tr>
<td colspan="3"><a href="">Add Additional Funding
Sources</a><br>
</td>
</tr>
</table>
</form>
</body>
</html>