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

adding values in text boxes

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
if i have three text boxes and i want to add the 3 values togeather and say if they are greater than a certain number. How would i go about doing this.
 
Well firstly, since they are input as Strings - you will need to convert them to Numbers. If you do not, they will be added together like strings (concatenated) - meaning 1+2=12.

function add(form){
var No1 = new Number(form.v1.value);
var No2 = new Number(form.v2.value);
var No3 = new Number(form.v3.value);

var answer = No1+No2+No3;
form.answer.value = answer;
if(answer > 10){
alert("Numbers add to more than 10")
}

}


Where the function is called from a buton like this:

<input type=&quot;button&quot; onClick=&quot;add(this.form)&quot;...>

And the v1,v2,v3 are just the names of the three text fields for input.

The this.form attribute holds the form object for the element we call it on - in this case the button - so we are sending the function the complete form object to operate on. ;-)
b2 - benbiddington@surf4nix.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top