473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to get value of different checkboxes and calculate value of those checked checkbo

Hi, I just starting learning javascript by myself and have quick question if anyone can help. I already checked recent post and couldn't find any similar one.
To get value of different check boxes and calculate value of those checked check boxes and show in the input field .It is working for default values more than one it doesn't work can anyone help? here id my code
Expand|Select|Wrap|Line Numbers
  1. <html> <head> <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script type="text/javascript">
  2.     $(document).ready(function () {
  3.         var sumOfVals = 0;
  4.         $('#posscanner').click(function () {
  5.             $("#txtposscanner").toggle();
  6.         });
  7.         $('#surveillancecam').click(function () {
  8.             $("#txtsurveillancecam").toggle();
  9.         });
  10.         $('#lotteryscanner').click(function () {
  11.             $("#txtlotteryscanner").toggle();
  12.         });
  13.         $('#invoincescanner').click(function () {
  14.             $("#txtinvoincescanner").toggle();
  15.         });
  16.         $('#networkscanner').click(function () {
  17.             $("#txtnetworkscanner").toggle();
  18.         });
  19.         $('#txtposscanner').keyup(function () {
  20.             var txtscval = $("#txtposscanner").val();
  21.             var chkscval=$('#posscanner').val();
  22.             var dataps = txtscval * chkscval;
  23.          /*   $('.chskboxva:checked').each(function () {
  24.                 var data = $(this).val();
  25.                 sumOfVals = sumOfVals + parseInt($(this).val());
  26.             });
  27.             var result = (txtscval * chkscval) + sumOfVals - chkscval;*/
  28.             $("#price").val(dataps);
  29.         });
  30.         $('#txtsurveillancecam').keyup(function () {
  31.             var txtscval = $("#txtsurveillancecam").val();
  32.             var chkscval = $('#surveillancecam').val();
  33.             var datassc = txtscval * chkscval;
  34.             $("#price").val(datassc);
  35.         });
  36.         $('#txtlotteryscanner').keyup(function () {
  37.             var txtscval = $("#txtlotteryscanner").val();
  38.             var chkscval = $('#lotteryscanner').val();
  39.             var datals = txtscval * chkscval;
  40.             $("#price").val(datals);
  41.         });
  42.         $('#txtinvoincescanner').keyup(function () {
  43.             var txtscval = $("#txtinvoincescanner").val();
  44.             var chkscval = $('#invoincescanner').val();
  45.             var datais = txtscval * chkscval;
  46.             $("#price").val(datais);
  47.         });
  48.         $('#txtnetworkscanner').keyup(function () {
  49.             var txtscval = $("#txtnetworkscanner").val();
  50.             var chkscval = $('#networkscanner').val();
  51.             var datans = txtscval * chkscval;
  52.             $("#price").val(datans);
  53.         });
  54.        /* var pizza_toppings = 0;
  55.         $('input[type="checkbox"]').on('click', function (e) {
  56.             if (this.checked) {
  57.                 pizza_toppings += eval(this.value);
  58.             } else {
  59.                 pizza_toppings -= eval(this.value);
  60.             }
  61.  
  62.             $("#price").val(pizza_toppings);
  63.         });
  64.     });*/
  65.  
  66.    var $inputs = $('input[type="checkbox"]')
  67.     $inputs.on('change', function () {
  68.         var sum = 0;
  69.         $inputs.each(function() {
  70.         // iterate and add it to sum only if checked
  71.            if(this.checked)
  72.                sum += parseInt(this.value);
  73.         });
  74.         $("#price").val(sum);
  75.     });
  76. });
  77.  
  78.  
  79. </script> </head> <body><p id="pizza_toppings"> <label class="lblitems"><input type="checkbox" class="chskboxva" id="posscanner" name="POS Scanner" value="2"> POS Scanner</label> <input class="txt" id="txtposscanner" style="width: 40px; display: none;" type="text" value="1" name="txt" /><br /> <label class="lblitems"><input type="checkbox" class="chskboxva" id="surveillancecam" name="Surveillance Camera" value="3"> Surveillance Camera</label> <input id="txtsurveillancecam" class="txt" style="width: 40px; display: none;" type="text" value="1" name="txt" /><br /> <label class="lblitems"><input type="checkbox" class="chskboxva" id="lotteryscanner" name="Lottery Scanner" value="4"> Lottery Scanner</label> <input id="txtlotteryscanner" class="txt" style="width: 40px; display: none;" type="text" value="1" name="txt" /><br /> <label class="lblitems"><input type="checkbox" class="chskboxva" id="invoincescanner" name="Invoice Scannerr" value="5"> Invoice Scannerr</label> <input id="txtinvoincescanner" class="txt" style="width: 40px; display: none;" type="text" value="1" name="txt" /><br /> <label class="lblitems"><input type="checkbox" class="chskboxva" id="networkscanner" name="Network Router" value="6"> Network Router</label> <input id="txtnetworkscanner" class="txt" style="width: 40px; display: none;" type="text" value="1" name="txt" /> </p> <p>
  80.     Calculated Price:
  81.     <input type="text" name="price" id="price" /> </p></body> </html>
Jan 23 '14 #1
1 1400
Dormilich
8,658 Expert Mod 8TB
problem #1, checkboxes don’t change their value, only their checked state, so change is the wrong event type.

PS. how to get only the checked checkboxes:
Expand|Select|Wrap|Line Numbers
  1. $('input[type="checkbox"]:checked')
Jan 24 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert...
6
by: Kevin Frey | last post by:
Hello, I have a table which contains some nullable columns. I want to write a single query, which can be prepared (ie. prepared statement), that can handle null or non-null values for the where...
16
by: Kevin Goodsell | last post by:
What do you think is the best way to handle a compiler warning about comparing an unsigned value to a signed value? Cast to silence it? Disable that warning altogether? Or just live with it? On...
1
by: Martin | last post by:
Dear Group Sorry for bothering you again but I need expert advice on this. I have placed a HTML textbox on my aspx form and converted it to run as a server control. At some point in my code I...
6
by: Aussie Rules | last post by:
Hi, I have a datepicker that show a calender. The user picks a date and the time component is always 00:00. I then have a drop down that provides a list of times, (10:00, 11:00 etc), and I...
4
by: Matt Kruse | last post by:
According to standards, if an option is selected and it has no VALUE attribute, the contents of the option tag is to be submitted. So: <select name="sel"> <option selected>Value</option>...
3
by: ll | last post by:
I'm working with the script below which works with FF but not with IE 7. To clarify, the part of the code which isn't working in IE 7 is putting the value of variable y in the "totalString" input...
1
by: arunbojan | last post by:
Hi All, I have an editable datagrid with 10 columns, one of those is ddl column which I used for showing the status.... By default all the columns are readonly, when user clicks edit,...
1
dlite922
by: dlite922 | last post by:
I spent a couple hours on this and wanted to save someone else a few hours. This function takes a percentage value and calculates the RGB color. This is useful for a graph or table in which you...
5
by: madhanrajesh210002 | last post by:
I'm trying to calculate dynamic formula value for dynamic inputs, that is user inputs and formulla all will be given by user, i tried in php, but failed , codes as follows, $a=10; //$a -...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.