Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

count or sum of checked boxes in a table

Posted on 2013-05-09
4
Medium Priority
?
266 Views
Last Modified: 2013-05-09
I have a table setup with two columns (side1 and side2).  
<table border = "1" width="100%" id="Table1">
<td class = 'Side1'>checkbox is here</td>  <td class = 'Side2'>checkbox is here</td>


I need to make sure that at least one checkbox is checked on side1 and at least one is checked on side2.

I tried the following but I am getting a syntax error somewhere...I don't even know if this would be the correct way to do it.

        var sideTotal = [];
        $('#table1 .Side1').each(function(){
            sideTotal[0]+= $(this) :checked.val();
        });
        alert(sideTotal[0]);

Open in new window

0
Comment
Question by:KCTechNet
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 39153987
var sideTotal = [];
var n = $('.Side1,.Side2', '#table1').size();
alert(n);
sideTotal[0] = n;

Open in new window

0
 

Author Comment

by:KCTechNet
ID: 39154028
Would't this return the count of all "Side1" and "Side2", regardless of checked or not?

Well, if it does, then I have a bigger problem because I get an alert with zero.
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 39154037
Try this :
var sideTotal = [];
var n = $('.Side1,.Side2', '#Table1').find(":checkbox:checked").size();
alert(n);
sideTotal[0] = n;

Open in new window

0
 

Author Comment

by:KCTechNet
ID: 39154062
I needed each one seperately, but that gave me what I needed.  Thanks.
        var canContinue = 1;
        var n        
            n = $('.Side1', '#Table1').find(":checkbox:checked").size();
            if (n == 0) {
                alert('You need to select at least one item in the first column');
                canContinue = 0;
            }
     
            n = $('.Side2', '#Table1').find(":checkbox:checked").size();
            if (n == 0) {
                alert('You need to select at least one item in the second column');
                canContinue = 0;
            }

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question