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

Reseting only changed form var

Status
Not open for further replies.

RoseDa

MIS
Joined
Aug 26, 2004
Messages
12
Location
US
I have a javascript that adds up inputs from a form. When the total is >= 110 it sends an alert that the total can not be over 100. This all works, but after the alert the form option stays at the value that put the total over 100. How do I only reset the variable that is changing, not all the variables?

Here is the code for the function.
Code:
//Calculates the total of all selections
function GetTotal()
{
    //get variables from form and makes them integers
    var Ami = parseInt(document.voting.Ami.value); 
    var Chad = parseInt(document.voting.Chad.value); 
    var Chris = parseInt(document.voting.Chris.value); 
    var Eliza = parseInt(document.voting.Eliza.value);
    var John_K = parseInt(document.voting.John_K.value);
    var Julie = parseInt(document.voting.Julie.value);
    var Lea = parseInt(document.voting.Lea.value);
    var Leann = parseInt(document.voting.Leann.value);
    var Lisa = parseInt(document.voting.Lisa.value);
    var Rory = parseInt(document.voting.Rory.value);
    var Scout = parseInt(document.voting.Scout.value);
    var Twila = parseInt(document.voting.Twila.value);

  
     //adds all variables together to get total
var Gtotal = Ami + Chad + Chris + Eliza + John_K + Julie + Lea + Leann + Lisa + Rory + Scout + Twila;
           
     //makes the form element total equal to variable total
     if (Gtotal >= 110){ 
       alert('Total can not be more than 100!');
       }
     else {
       document.voting.total.value = Gtotal;
       }
}

Hope someone can help.

Yours,
Dale Rose
 
Try this:

function GetTotal()
{
var names=['Ami', 'Chad', 'Chris', 'Eliza', 'John_K', 'Julie', 'Lea', 'Leann', 'Lisa', 'Rory', 'Scout', 'Twila'];

var Gtotal = 0;

for (var ni=0;ni<names.length ni++)
{
Gtotal += parseInt(document.voting.elements[names[ni]].value);
if (Gtotal >= 110)
{
alert('Total can not be more than 100!');
document.voting.elements[names[ni]].focus();
document.voting.elements[names[ni]].select();
return;
}
}
document.voting.total.value = Gtotal;
}
 
I cut and pasted the code in and it does not work.
The warning never gets activated, because Gtotal never gets to 110.

I think I need to explain a little more.
The selections for each name in the form is nothing to 100 in increments of 10.

So when someone changes the form it updates the total via adding up all the form variables and then places it in the total box in the form.

So I want to keep the total calculating of the total going, but if the user selects more than 100 then it warn them and then resets the selection they where changing to 0.

Hope this makes it clearer.

Yours,
Dale Rose
 
troll's code is missing a semicolon:

Code:
for (var ni = 0; ni < names.length[red];[/red] ni++)

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
Still did not work with the syntex correction. The script would reset the first ni name that made Gtotal more than 100, not the one the form was using. [sad]

I did take the concept and did some tweaking. I added a condition to the function statement and then made the form pass the element name to the function.

The working code looks like this:

Code:
function GetTotal(xName)
{
var names=['Ami', 'Chad', 'Chris', 'Eliza', 'John_K', 'Julie', 'Lea', 'Leann', 'Lisa', 'Rory', 'Scout', 'Twila'];

var Gtotal = 0;

for (var ni=0; ni<names.length; ni++)
  {
  Gtotal += parseInt(document.voting.elements[names[ni]].value);
//Test is Gtotal is more than 100
  if (Gtotal >= 110)
    {
    alert('Total can not be more than 100!');
    document.voting.elements[xName].value = 0;
    return;
    }
  }
document.voting.total.value = Gtotal;
}

In the HTML form I had the use single quotes instead of the normal quotes.

The HTML code is:

Code:
<select name="Chad" onchange="GetTotal('Chad')">

Now the form and script work great. [2thumbsup]

Thanks for the help.

Yours,
Dale Rose
 
Looks like I got sloppy late at night. :)# (bearded smiley face)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top