Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need function to work with SelectBox

Status
Not open for further replies.
Apr 23, 2004
2
0
0
US
Hi,

I need this function to work with select boxes instead of text boxes. I have searched and tried for days....Eventually, the form will have many rows. Here is the code that I have:

function updateSums(row)
{
var dfe = document.forms[0].elements;
var box1 = dfe['txtcol1_'+row];
var box2 = dfe['txtcol2_'+row];
var box3 = dfe['txtcol3_'+row];

var actual1 = false;
var actual2 = false;

var val1 = box1.value;
if(val1 && !isNaN(val1))
{

val1=parseInt(val1);
actual1=true;
}
//end if
else
{
val1 = 0;
actual1=false;
}//end else

var val2 = box2.value;
if(val2 &&!isNaN(val2))
{
val2 = parseInt(val2);
actual2 = true;
}//end if
else
{
val2=0;
actual2=false;
}//end else

var val3 = box3.value;
var boxAll = dfe['grandTotal'];
var total = boxAll.value;

if(actual1 && actual2)
{
if(val3)

total-=val3;
val3 = val2-val1;
box3.value= val3;

if(val3 && !isNaN(val3))
{
val3=parseInt(val3);
if(total)
total = parseInt(total) + val3;
else
total = val3;
}//end if

boxAll.value = total;
}//end if
}//end updateSums(var)
</script>
<body>
<form>
<table id='sumTable'>
<td>
<input type='text' name='txtcol1_1' onblur='updateSums(1);'/> </td>
<td>
<input type='text' name='txtcol2_1' onblur='updateSums(1);'/> </td>
<td>
<input type='text' name='txtcol3_1' readonly='readonly'/>
</td>
<tr>
<td>
<input type='text' name='txtcol1_2' onblur='updateSums(2);'/> </td>
<td>
<input type='text' name='txtcol2_2' onblur='updateSums(2);'/> </td>
<td>
<input type='text' name='txtcol3_2' readonly='readonly'/>
</td>
</tr>
<tr>
<td colspan='2' align='right'>total:</td>
<td>
<input type='text' name='grandTotal' readonly='readonly'/>
</td>
</tr>
</table>
</form>
</body>
</html>



 
Can you post the code that doesn't work? I would suggest something like:
Code:
<html>
	<head>
		<script>
			var n_current_value = 0;
			
			function set_current(o_field)
			{
				n_current_value = o_field.options[this.selectedIndex].value;
			}
			
			function updateSums(o_field)
			{
				document.getElementById("total") -= n_current_value + o_field.options[this.selectedIndex].value;
			}
		</script>
    <body>
	    <form>
			<table id='sumTable'>
				<tr>
					<td>
						<select id="col1_1" onchange="updateSums(this);" onfocus="set_current(this);>
							<option>0</option>
							<option>1</option>
							<option>2</option>
							<option>3</option>
						</select>
					</td>
					<td>
						<select id="col2_1" onchange="updateSums(this);" onfocus="set_current(this);>
							<option>0</option>
							<option>1</option>
							<option>2</option>
							<option>3</option>
						</select>
					</td>
					<td>
					<input type='text' name='col3_1' readonly='readonly'/>
					</td>
				</tr>
				<tr>
					<td>
						<select id="col1_2" onchange="updateSums(this);" onfocus="set_current(this);>
							<option>0</option>
							<option>1</option>
							<option>2</option>
							<option>3</option>
						</select>
					</td>
					<td>
						<select id="col2_2" onchange="updateSums(this);" onfocus="set_current(this);>
							<option>0</option>
							<option>1</option>
							<option>2</option>
							<option>3</option>
						</select>
					</td>
					<td>
					<input type='text' name='col3_2' readonly='readonly'/>
					</td>
				</tr>
				<tr>
					<td colspan='2' align='right'>total:</td>
					<td><input type='text' name='total' readonly='readonly' value="0"/></td>
				</tr>
			</table>
		</form>
	</body>
</html>

Note: this code is untested, but should point you in the right direction.

Good luck!
Mike

Take Care,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top