Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

simple java script problem

I do not work to often in java script so i need help with something very simple:

I have to there text boxes:

I need to take txtQuan X  txtUnitPrice = txtTotalPrice

This needs to happen when the txtQuan is changed.
0
vbnetcoder
Asked:
vbnetcoder
  • 9
  • 6
  • 2
2 Solutions
 
Justin MathewsCommented:
You need to add an onchange() event handler to the <INPUT> field for txtQuan. To do that:

1. Change your HTML <INPUT> tag for txtQuan to add onchange handler as:
<INPUT name='txtQuan' onchange='calcTotal()'>
2. Define calcTotal() JavaScript function  in your HTML
eg:
<HTML>
<HEAD>
<SCRIPT language='javascript'>
function calcTotal()
{
   
document.forms['<your form name here>'].elements['txtTotalPrice'].value =
}
0
 
darkyin87Commented:
I would use onBlur event n chk whether the value has changed and then calculate the result.
0
 
vbnetcoderAuthor Commented:
darkyin87: How would i calculate the result?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Justin MathewsCommented:
Sorry, hit submit too soon.

You need to add an onchange() event handler to the <INPUT> field for txtQuan. To do that:

1. Change your HTML <INPUT> tag for txtQuan to add onchange handler as:
<INPUT name='txtQuan' onchange='calcTotal()'>

2. Define calcTotal() JavaScript function  in your HTML
eg: (Assuming your form element name is 'calcform')
<HTML>
<HEAD>
<SCRIPT language='javascript'>
function calcTotal()
{    
document.forms['calcform'].elements['txtTotalPrice'].value = document.forms['calcform'].elements['txtQuan'].value * document.forms['calcform'].elements['txtUnitPrice'].value;
return true;
}
</SCRIPT>
.
.
.
0
 
vbnetcoderAuthor Commented:
My form happens to be a asp.net user control. do you know what i would put in for form name?
0
 
darkyin87Commented:
function onBlur()
{
document.getElementById('txtTotalPrice').value = document.getElementById('txtUnitPrice ').value x document.getElementById('txtQuan ').value
}
0
 
Justin MathewsCommented:
If the form is the first form in your HTML (or the only form) you can also say document.forms[0] instead of document.forms['calcform']
0
 
vbnetcoderAuthor Commented:
darkyin87:

it does not like the X in you code. it says it expects ;
0
 
Justin MathewsCommented:
For multiplication it should be * (asterisk) not X.
0
 
vbnetcoderAuthor Commented:
Can i call it like this?

OnChange="calcTotal()"
0
 
vbnetcoderAuthor Commented:
OnChange='calcTotal()' did not work
0
 
vbnetcoderAuthor Commented:
This is what i am doing

<telerik:RadNumericTextBox ID="txtQuantity" TextChanged="calcTotal()" runat="server" Width="30px"                                        
</telerik:RadNumericTextBox></td>

and it does not seem to be calling calc total

I am trying to use the TextChanged event because it is outlined for my control here

http://www.telerik.com/help/aspnet/input/radinput-telerik.webcontrols.radnumerictextbox_members.html
0
 
Justin MathewsCommented:
You can use either single quote (') or double quote (").
0
 
vbnetcoderAuthor Commented:
Am i calling it wrong?
0
 
Justin MathewsCommented:
If it is a third party control and is scriptable, try something like below in your <BODY onload>:

document.getElementById('txtQuantity').TextChanged = calcTotal;

eg:
<BODY onload="document.getElementById('txtQuantity').TextChanged = calcTotal">
0
 
vbnetcoderAuthor Commented:
It is asp.net user control it does not have a <body> tag
0
 
vbnetcoderAuthor Commented:
you both helped so thank you
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now