Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamic count of checkboxes in a table

Posted on 2012-12-23
3
Medium Priority
?
1,539 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 1000 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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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?
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

688 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