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

Adding the value of a textbox - extreme newbie toJavaScript

Status
Not open for further replies.

NoOneInParticular

Programmer
Dec 1, 2005
19
CA
I'm trying to add a value from one textbox, to another value, and display the total in a second textbox. The value in the first checkbox depends on a what checkboxes the user checks.
Each checkbox has a different value, and the first textbox displays the total dollar value of the diff. checkboxes that are checked. The second textbox displays the value of the first textbox plus 10.

THE PROBLEM: I can't get the values to add together. I can't extract the value of the first textbox to add 10 to it, and display it in the second textbox.[ponder]

Any help is welcome!!!

Here is my code:

function Calculate(){

form1.trinkets.value = ""
var totalvalue = "";

var totaltrinkets = "";

if (form1.check1.status == 1){

total1 = total1 + 75;

form1.totalvalue1.value = total1;

} else {}
if (form1.check2.status == 1){

total1 = total1 + 200;

form1.totalvalue1.value = total1;

} else {}

totalvalue = form1.totalvalue1.value + 10;

form1.total2.value = totalvalue;
}
 
Here's what I would do in your situation to debug this.

First, make sure the checked value is ever going to be what you are testing against. Change this:

Code:
if (form1.check1.status == 1)

to this:

Code:
alert(form1.check1.status);
if (form1.check1.status == 1)

If the value alerted is not 1, then change the 1 to be whatever is alerted (probably the boolean value true... and while 1 and true can be the same, it never hurts to put explicit values in your code).

Now - can you tell us what your code is doing, as well as what it is not doing?

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi, thanks for your help! I was able to figure it out. My problem was: I was trying to extract a value from the textbox as a numeric value, but it was coming out as a string. I solved the problem by:

function Calculate(){
if (form1.check1.status == 1){
totalvalue1 = totaltotalvalue1 + 20;
form1.total1.value = totaltotalvalue1;
} else {}

if (form1.check2.status == 1){
totalvalue1 = totaltotalvalue1 + 30;
form1.total1.value = totaltotalvalue1;
} else {}

totalvalue2 = totalvalue1 + 10;
form1.total2.value = totalvalue2;
}

This works: i.e. it adds the values together, coming out with (if both checkboxes are checked) 50 in total1 and 60 in total2. What it was doing before was: 2030 in total1, and 203010 in total2.

Um, what would your change do for me? I'm brand-new to this, so I don't really know much.
 
It was just a suggestion to change the way your values were being evaluated to see if it made a difference. My next suggestion would have been to use parseInt - which by the sound of things, would probably have fixed your problem as well.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top