Solved

Dynamic count of checkboxes in a table

Posted on 2012-12-23
3
1,347 Views
Last Modified: 2012-12-27
I have a table that display a jquery checkbox at the end of every row.  I would like to display the total number of checkboxes that are checked in the last row of the table.  Here is the table:

<table border="1">
                <tr>
	                <th></th>
                    <th style="vertical-align:middle;">Number</th>
                    <th style="text-align:left; vertical-align:middle;">Owner</th>
                    <th style="text-align:left; vertical-align:middle;">Address</th>
                    <th style="vertical-align:middle;">Code</th>
                    <th style="vertical-align:middle;">Class</th>
                    <th style="vertical-align:middle;">Filing Fee</th>
                    <th style="text-align:center; width:70px;">Review ?<br /><span style="text-decoration:underline; cursor:pointer;" onclick="select_all_com()">All</span> &nbsp;&nbsp;&nbsp;
					<span style="text-decoration:underline; cursor:pointer;" onclick="select_none_com()">None</span></th>
                </tr>

                <?php foreach ($property['commercial'] as $key => $value): ?>
                    <tr>
                    	<th><?php echo str_pad($row++, 2, '0', STR_PAD_LEFT); ?></th>
                        <td><a href= "<?php echo site_url() . "propertydetail/viewpropertydetail/" . $value['property#']; ?>"><?php echo Roll($value['property#']); ?></a></td>
                        <td style="text-align:left"><?php echo $value['Property Owner']; ?> </td>
                        <td style="text-align:left"><?php echo $value['Property Address']; ?></td>
                        <td style="text-align:middle" ><?php echo number_format($value['PC'],0); ?></td>
						<td><?php echo $value['Class']; ?></td>
                        <td><?php echo money_format('$%.0i', 140); ?></td>
                        <td id="com" style="padding-left:23px;">
                        	<input type="checkbox" name="file_com_appeal" class="com-checkbox" value="<?php echo $value['property#'] ?>">                            
                        </td>                                            
                    </tr>
                <?php endforeach; ?>
                    <th colspan="6" style="text-align:right; padding:1% 3% 1% 0%;"><h4>Total</h4></th>
                    <th><?php echo "NEED COUNT OF THE CHECKED CHECKBOXES HERE"; ?></th>
                    <th></th>
            </table>

Open in new window


Could someone please help me to understand how to keep track of the number of checkboxes that are checked?  By the way, this needs to update itself with each checkbox that is changed.

Thank you.
0
Comment
Question by:DanielAttard
3 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 250 total points
ID: 38717621
One of the attributes of a checkbox is checked which is boolean.  A count of the number that are true will always yield the count.  If you establish the count at load time, then just capture the click event that occurs whenever a checkbox is checked or unchecked.  Then al you have to to is test the state and add or subtract one from the total.  The total cell can contain a simple text input and you just update the value attribute from the javascript variable where you are keeping the count.

Don't try and do arithmetic with the input value because it is a string.

Cd&
0
 
LVL 11

Assisted Solution

by:mcnute
mcnute earned 250 total points
ID: 38718600
In addition to COBOdinosaur's solution here a code snippet on how to do it:

$(function(){
    var total = 0;
    $('input[type=checkbox]').live('mouseup', function(){
        if ($(this).attr('checked') == true) {
             total++;
        } else {
             total--;
        }
      $('input[name=totalcheckboxes]').val(total);
   });
});

Open in new window


This is checking for a mouseup event on checkbox and then looks if checked is true or false. On true it adds one to total, if false it subtracts one on total and then it show the value in a text input with the name totalcheckboxes.
0
 

Author Closing Comment

by:DanielAttard
ID: 38724753
Thanks for the tips.  Very helpful.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now