Solved

Dynamic count of checkboxes in a table

Posted on 2012-12-23
3
1,412 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This video teaches users how to migrate an existing Wordpress website to a new domain.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

828 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