how to get/set textbox values client side document.getElementByID

G

Guest

Hello,

I'm trying to set up an ASP.NET 2.0 form where the user enters values in
WebControls.TextBoxes for amount owing, interest and late fees and a
JavaScript function totals the three values and sets the value of a label
(lblTotal) to the sum of the three textbox values. The form is in a content
place holder.


I've added the following block of code in my form load.
The idea is to pass the three parameters to the update function:

txtAmount.Attributes.Add("onchange",
"update(document.getElementById('txtAmount.ClientID').value,
document.getElementById('txtLateFees.ClientID').value,
document.getElementById('txtInterest.ClientID').value);");
txtInterest.Attributes.Add("onchange",
"update(document.getElementById('txtAmount.ClientID').value,
document.getElementById('txtLateFees.ClientID').value,
document.getElementById('txtInterest.ClientID').value);");
txtLateFees.Attributes.Add("onchange",
"update(document.getElementById('txtAmount.ClientID').value,
document.getElementById('txtLateFees.ClientID').value,
document.getElementById('txtInterest.ClientID').value);");


function update(amt, itr, ltf)
{
var total = 0.00;
if(amt)
total += parseFloat(amt) ;
if(itr)
total += parseFloat(itr) ;
if(ltf)
total += parseFloat(ltf);
document.getElementById('ctl00$ContentPlaceHolder1$lblTotal').value =
total.toFixed(2);
}
</script>


The code appears to be failing with "object required" at this point. When
I debug in VS 2005 I get an "object required" error at the following code
block. It doesn't appear to recognize txtAmount.ClientID as a valid pointer
to the control.

<td style="width: 2px">
<input
name="ctl00$ContentPlaceHolder1$txtAmount" type="text"
id="ctl00_ContentPlaceHolder1_txtAmount"
onchange="update(document.getElementById('txtAmount.ClientID').value,
document.getElementById('txtLateFees.ClientID').value,
document.getElementById('txtInterest.ClientID').value);"
style="height:14px;width:100px;" /></td>

I'm a bit of a Javascript newbie and would sincerely appreciate any help I
could get here.

Thanks in advance,

Andre Ranieri
 
B

bruce barker \(sqlwork.com\)

ClientID is a serverside property, javascript knows nothing about it. you
have to build client script with the actual value of ClientID

string script ="var id = document.getElementById('" + myControl.ClientId +
"');";

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,811
Latest member
SaulFernan

Latest Threads

Top