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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change text box value according to another text box

Status
Not open for further replies.

kevpho

Programmer
Jun 9, 2003
47
CA
Hi there,

I've been trying to update the value of one textbox by using the onChange event when another textbox is modified. For now, I just want it to work so I'm trying to copy the exact value without any calculation, but for some reason it's not working. I suppose there's something wrong with my syntax, but I can't find it out. Here's the code:

Code:
<script language="Javascript">
<!--
	function updateX4(original)
	{
//The value of the field to update is always just 1 more than the incoming original variable		
var toChange = new String(parseInt(original.name) + 1);
		document.forms['pay'][toChange].value = original.value;
	}
-->
</script>

<form name="pay" action="paycreate.asp" method="post">
<input type="text" name="1000" onChange="updateX4(this)">
<input type="text" name="1001">
</form>

This is just an example, but my page uses asp and creates a number of form fields so the names will vary. The error is somewhere in the "document.forms['paysheet']....." line but I've tried many different combinations and keep getting an object expected error. If I just use alerts all the numbers I want are passed properly into and throughout the function.

Any help would be greatly appreciated. Thanks!

Kevin
 

It is your (illegal) use of form element names beginning with numbers that it causing the problem. Names should never begin with numbers. Changing them to something like "n1000", "n1001", etc, and then modifying your code to something like this:

Code:
<html>
<head>
<script language="Javascript">
<!--
	function updateX4(original) {
		//The value of the field to update is always just 1 more than the incoming original variable
		var toChange = parseInt(original.name.slice(1), 10) + 1;
        document.forms['pay']['n' + toChange].value = original.value;
	}
//-->
</script>
</head>
<body>
<form name="pay" action="paycreate.asp" method="post">
<input type="text" name="n1000" onChange="updateX4(this)">
<input type="text" name="n1001">
</form>
</body>
</html>

works for me.

Hope this helps,
Dan
 
Hi Dan,

Thanks for your help. I didn't realize that I was using illegal form names. Works just fine now.

Kevin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top