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

Adding txtboxes on change using Javascript

Status
Not open for further replies.

mcallaghan

IS-IT--Management
Mar 22, 2005
43
0
0
US
I have a simple little form wearing i am trying to add 2 txtbox values and put it in a 3rd textbox. When the value change the third box will change. Can anyone explain what I am doing wrong.

Thanks for all your help.

<script type="text/javascript" language="javascript">
function calcVals()
{
var r = document.getElementById("JanSpendTextBox").value;
var r = document.getElementById("JanTaxTextBox").value;
var grandTotal = r + m;
document.getElementById("JanTotalTextBox").value=grandTotal;

}
</script>

<form id="form1" name="form1" runat=server method="post" action="" >
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DefaultMode="Insert"
Width="320px">
<InsertItemTemplate>
JanSpend:
<asp:TextBox ID="JanSpendTextBox" runat="server" onkeyup="javascript:calcVals();" Text='<%# Bind("JanSpend") %>'></asp:TextBox><br />


JanTax: &nbsp; &nbsp;
<asp:TextBox ID="JanTaxTextBox" runat="server" onkeyup="javascript:calcVals();" Text='<%# Bind("JanTax") %>'></asp:TextBox><br />

JanTotal:&nbsp; &nbsp;<asp:TextBox ID="JanTotalTextBox" runat="server" autopostback="true" Text='<%# Bind("JanTotal") %>'></asp:TextBox>&nbsp;<br />
 
Code:
var r = document.getElementById("JanSpendTextBox").value;
var r = document.getElementById("JanTaxTextBox").value;
this is wrong on 2 levels.
1 your are overriding r and never assigning m a value.
2. if you use master pages, user controls or any other nested controls it will never find the control. you need to pass the clientid to javascript.

Code:
function calcVals()
{
    var r = document.getElementById('<%=JanSpendTextBox.ClientID%>').value;
    var m = document.getElementById('<%=JanTaxTextBox.ClientID%>').value;
    var grandTotal = r + m;
    document.getElementById('<%=JanTotalTextBox%>').value=grandTotal;
    
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Im getting the error that my 2 variables r and m arent declared. Am i missing something.
 
Since this is a javascript issues and not ASP.NET, I suggest you post here: forum216
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top