Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-11-2013, 07:29 AM   PM User | #1
billboy
New Coder

 
Join Date: Feb 2013
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
billboy is an unknown quantity at this point
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

Last edited by billboy; 02-11-2013 at 07:34 AM..
billboy is offline   Reply With Quote
Old 02-11-2013, 08:08 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-11-2013 at 08:23 AM..
Philip M is offline   Reply With Quote
Old 02-11-2013, 08:49 AM   PM User | #3
billboy
New Coder

 
Join Date: Feb 2013
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
billboy is an unknown quantity at this point
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
billboy is offline   Reply With Quote
Old 02-11-2013, 08:52 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by billboy View Post
Thank you so much for taking the time to help, it is greatly appreciated
Glad to be able to help!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:30 AM.


Advertisement
Log in to turn off these ads.