CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Total Form Input Checkbox (http://www.codingforums.com/showthread.php?t=287437)

billboy 02-11-2013 07:29 AM

Total Form Input Checkbox
 
I have a table with 5 rows consisting of 5 input boxes that a user will enter a number in and i have 5 checkboxes in the next column

I want to be able to total based on wether the checkbox is checked



Code:

function total_credit_payoff()
{
if document.getElementById("debtpayoff1").checked{
 othercredit1 = parseFloat(document.getElementById("credC1").value);
 othercredit2 = parseFloat(document.getElementById("credC2").value);
 othercredit3 = parseFloat(document.getElementById("credC3").value);
 othercredit4 = parseFloat(document.getElementById("credC4").value);
 auto1 = parseFloat(document.getElementById("auto1").value);
 totalcreditbal = othercredit1 + othercredit2 + othercredit3 + othercredit4 + auto1
}

In other words if the checkbox next to the input field is checked then the amount gets added to the total otherwise if the box is NOT checked it does not get added

Thanks

Philip M 02-11-2013 08:08 AM

Here you are:-

Code:

<html>
<head>
</head>
<body>

<form id = "myform">
<input type = "text" name = "txt1"><input type = "checkbox" name= "chk1" ><br>
<input type = "text" name = "txt1"><input type = "checkbox" name= "chk1" ><br>
<input type = "text" name = "txt1"><input type = "checkbox" name= "chk1" ><br>
<input type = "text" name = "txt1"><input type = "checkbox" name= "chk1"><br>
<input type = "text" name = "txt1"><input type = "checkbox" name= "chk1" ><br>

<input type = "button" value = "Get Total" onclick = "addemup()">
</form>

<script type = "text/javascript">

function addemup() {
var total = 0;
var count = 0;
var f = document.getElementById("myform");
for (var i=0; i<f.chk1.length; i++) {
if (f.chk1[i].checked == true) {
var x = f.txt1[i].value *1 || 0;  // 0 if a NaN entered
f.txt1[i].value = x;  // write it back to the field
total = total + x;
count++;
}
}

if (count == 1) {
alert ("The value of the one textbox you have checked is " + total);
}
else if (count>1) {
alert ("The total of the " + count + " textboxes you have checked is " + total);
}
}

</script>

</body>
</html>


Looking after up to six kids will be hard. Staff have only two pairs of hands, after all. - Presenter BBC Radio 2

billboy 02-11-2013 08:49 AM

Thanks I worked it out with long series of If Else statements
but I new there had to be a better way

Your way is much cleaner and a good function I can learn from and implement in other parts of my program.

Thank you so much for taking the time to help, it is greatly appreciated

Philip M 02-11-2013 08:52 AM

Quote:

Originally Posted by billboy (Post 1312507)
Thank you so much for taking the time to help, it is greatly appreciated

Glad to be able to help! :)


All times are GMT +1. The time now is 05:18 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.