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

Adding Dollar Values in 2 Textboxes 1

Status
Not open for further replies.

Lambro

Programmer
Aug 22, 2001
258
US
I have 3 textboxes on a form. Two of them will have dollar amounts entered into them always in dollars and cents like so: 23.99

I want to have textbox1 plus textbox2 equal textbox3.

User enters 25.99 into textbox1, textbox2 will have a value of 5.00 already in it and this textbox will be read only. When the user tabs off textbox1 I want textbox3 to show the total of textbox1 plus textbox2. textbox3 will also be read only.
 
Use the onblur event:

Code:
<input type='text' name='textbox1' onblur='addEmUp();' />

...then:

Code:
function addEmUp()
{
 var sum = parseInt(document.formName.textbox1.value) + parseInt(document.formName.textbox2.value);
 
 document.formName.textbox3.value = Math.round(sum * 100)/100;
}

That little *100/100-thingie in the function above is to avoid the crazy decimal games JavaScript plays when adding decimal numbers together.

'hope that helps.

--Dave
 
use the onBlur event to execute:
Code:
document.forms["formName"].textbox3.value = parseFloat(document.forms["formName"].textbox1.value) + parseFloat(document.forms["formName"].textbox2.value)

give it a try and pos if it's not working

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Oops!

Change my parseInt's above to parseFloat's.

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top