Jump to content

Adding Up Within A Form


wright67uk

Recommended Posts

How can I change the totalscore() function, so that it outputs the total outputs of the first three functions in my code?

 

<script language="javascript" type="text/javascript">  
function addroll(){  
 var r =  document.getElementById("roll_1").value * 1;  
     r += document.getElementById("roll_2").value * 1;  
     r += document.getElementById("roll_3").value * 1;  
 document.getElementById("roll_total").value = r;   
 return false;  // or true after testing
}  

function addchip(){  
 var c =  document.getElementById("chip_1").value * 1;  
     c += document.getElementById("chip_2").value * 1;  
     c += document.getElementById("chip_3").value * 1;  
 document.getElementById("chip_total").value = c;   
 return false;  // or true after testing
}  

function addpitch(){  
 var p =  document.getElementById("pitch_1").value * 1;  
     p += document.getElementById("pitch_2").value * 1;  
     p += document.getElementById("pitch_3").value * 1;  
 document.getElementById("pitch_total").value = p;   
 return false;  // or true after testing
}  

function totalscore(){  
 var T = r+c+p;
 document.getElementById("total_score").value = T;   
 return false;  // or true after testing
}  
</script>

Link to comment
Share on other sites

heres the code i'm using;

 

<form>
<!-- ROLL -->
<input value="ROLL" type="text" class="heading" readonly style="border:0px"  />
<input onchange="addroll(this.form)" value=" " type="text" id="roll_1" autocomplete="off" />
<input onchange="addroll(this.form)" value=" " type="text" id="roll_2"  autocomplete="off" />
<input value=" " type="text" readonly style="border:0px" /><br/>

<input type="text" class="heading" readonly style="border:0px" />
<input onchange="addroll(this.form)" value=" " type="text" id="roll_3" autocomplete="off" />
<input onchange="totalscore(this.form)" onkeyup="totalscore(this.form);" value=" " type="text" name="roll_total" id="roll_total" class="subtotal" autocomplete="off" readonly="readonly" />
<input value="Roll Total" type="text" class="subheading" readonly style="border:0px" /><br/>

<!-- CHIP -->

<input value="CHIP" type="text" class="heading" readonly style="border:0px" />
<input onchange="addchip(this.form)" value=" " type="text" id="chip_1" autocomplete="off" />
<input onchange="addchip(this.form)" value=" " type="text" id="chip_2" autocomplete="off" />
<input value=" "    type="text" readonly style="border:0px" /><br/>

<input type="text" class="heading" readonly style="border:0px"/>
<input onchange="addchip(this.form)" value=" " type="text" id="chip_3" autocomplete="off" />
<input onchange="totalscore(this.form)" onkeyup="totalscore(this.form);" value=" " name="chip_total" type="text" id="chip_total" class="subtotal" autocomplete="off" readonly="readonly" />
<input value="Chip Total" type="text" class="subheading" readonly style="border:0px"/><br/>

<!-- PITCH  -->

<input value="PITCH" type="text" class="heading" readonly style="border:0px"/>
<input onchange="addpitch(this.form)" value=" " type="text" id="pitch_1" autocomplete="off" />
<input onchange="addpitch(this.form)" value=" " type="text" id="pitch_2" autocomplete="off" />
<input value=" " type="text" readonly style="border:0px" autocomplete="off" /><br/>

<input type="text" class="heading" readonly style="border:0px" />
<input onchange="addpitch(this.form)" value=" " type="text" id="pitch_3" autocomplete="off" />
<input onchange="totalscore(this.form)" value=" " name="pitch_total" type="text" id="pitch_total" class="subtotal" autocomplete="off" readonly="readonly" />
<input value="Pitch Total" type="text" class="subheading" readonly style="border:0px" /><br/>

<!-- TOTAL -->

<input               type="text" class="heading" readonly style="border:0px" />
<input value="TOTAL" type="text" class="total" readonly style="border:0px; margin-right:4px;"/>
<input value=" "     type="text" id="total_score" name="total_score" class="totalout"  readonly="readonly"/>
<input value=""    type="text" readonly style="border:0px" /><br/>

</form>

Link to comment
Share on other sites

<script language="javascript" type="text/javascript">  
function sumItem(item){
 t = 0
 for(i = 1; i <= 3; i++) 
    t+= (document.getElementById(item+"_"+i).value * 1);
 document.getElementById(item+"_total").value = t;   
 return t;  // or true after testing
} 

function totalscore(){  
 var T = sumItem('chip') + sumItem('pitch') + sumItem('roll');
 document.getElementById("total_score").value = T;   
 return false;  // or true after testing
}  
</script>

 

Although I'd probably split up the value change from the actual summation as its more logical. There's probably an easier way to do it as well but I'm only thinking about your specific needs. What I've offered isn't particularly extensible either but never the less.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.