0

how to make this "if the checkbox is checked add total + 10000 if uncheck none"
because in this situation everytime i check or uncheck the checkbox it adds 10000

function AmmenitiesCALC(value){ 
newValue = parseInt(value); 
document.formcheck.Total.value = newValue;
Total+=newValue;
document.formcheck.Total.value = Total; 
} 

<tr>
<th>AMMENITIES INCLUDED </th><td><input type="checkbox" name="Ammenities" value='10000' onClick="AmmenitiesCALC(this.value)"></td>
<td> </td>
<td> </td><th> Php 10,000 </th>
</tr>
4
Contributors
3
Replies
27
Views
4 Years
Discussion Span
Last Post by DavidB
0

So, I am not clear on exactly what your issue is, but based on what I read from your post, i assume that you want to add 10,000 when the checkbox is checked, and subtract 10,000 when you uncheck it? correct?

Here is a modified demo with more than one option so you can see how it adds or subtracts as you check/uncheck.

I'm not an expert at JavaScript either so there may be other more efficient ways to handle this...

See --> jsFiddle Demo

.

<!DOCTYPE html>
<html>
<head>
 <title>Demo</title>
</head>
<body>

<table>
 <tr>
  <td>Option 1</td>
  <td><input type="checkbox" name="option1" value='10000' onclick="myCalc(this)"></td>
  <td>10,000</td>
 </tr>
 <tr>
  <td>Option 2</td>
  <td><input type="checkbox" name="option2" value='5000' onclick="myCalc(this)"></td>
  <td>5,000</td>
 </tr>
</table>

<p>Total = <span id="Total"></span></p>

<script>
curVal = 0;
document.getElementById('Total').innerHTML = curVal;
function myCalc(e){
 newVal = parseInt(e.value);

 if (e.checked) {
  curVal = curVal + newVal;     
  document.getElementById('Total').innerHTML = curVal;
 } else {
  curVal = curVal - newVal;  
  document.getElementById('Total').innerHTML = curVal;
 } 
}
</script>
</body>
</html>

Edited by JorgeM

0

I do not see a conditional statement.
On line 4, I see Total+=newValue;, but it doesn't check first to see if checkbox checked. It adds 10000 to value regardless.

You should have code like the following:

if (checkbox checked){
  Total+=newValue;
}
else {
  do nothing;  // or whatever action should occur if checkbox unchecked
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.