0

I have a table that looks like this: http://jsfiddle.net/8SEz2/4/

Branch    StSeq#   Inv#  Invoice Date    Payable Amount     Pay?
Branch1     2       A2   11/11/2011        49,500.00       checkbox(unchecked)
Branch1     3       A3   11/11/2011        12,221.55       checkbox(checked)
Branch3     4       B1   11/11/2011        12,220.56       checkbox(unchecked)

What I'm trying to accomplish is when a row checkbox state is changed I need to count the number of checkboxes that are also checked that share the same branch name.

Ex. If I click on the first checkbox, (Branch1) I should get a popup with the branch checked count and in this case, it should be 2.

This is my jquery:

//Please refer to the jsFiddle for the class names
//used to track checked branches
$('input.payMe').change(function() {
    if ($(this).is(':checked')) {
    //cm enable
    var branchName = $(this).parent().prevAll("td.invoiceBranch").html().trim();
    var branchCount = 0;
    $('td.invoiceBranch').each(function(index) {
        if ($(this).html().trim() == branchName) {
            branchCount++;
        }
    });
        alert(branchCount);
    }
    else {
    //cm disable
        var branchName = $(this).parent().prevAll("td.invoiceBranch").html().trim();
        var branchCount = 0;
        $('td.invoiceBranch').each(function (index) {
            if ($(this).html().trim() == branchName) {
                //Needs to select the checkbox to get checked state.
                //Not sure if this is correct
                var inputHTML = $(this).nextAll("td.paymentCheckBox").first();
                if($(inputHTML).is(":checked")) {
                    branchCount++;
                }
            }
        });      
        alert(branchCount);
    }
}); 

Any ideas? I've been stumped for 2 days. Thanks!

2
  • Why not code the branchname as part of the checkbox ID or name ?? Commented Jan 6, 2012 at 8:40
  • I considered that, but it's not what I need. I have another table in that page that has it's rows disabled if the number of branched checked is 0. So if 1 out of two checkboxes of Branch1 is selected, my other table's rows get enabled, if it's zero, it needs to be disabled again. That's why I need the total checked count. Commented Jan 6, 2012 at 8:44

2 Answers 2

3

I think you should apply a tag for each checkbox with the name of branch

<input type="checkbox" class="payMe" branch="Branch1" ... />

Then the function should roughly be like

$(':checkbox.payMe').change(function() {
  alert($(':checkbox.payMe[branch="' + $(this).attr('branch') + '"]:checked').length);
});
Sign up to request clarification or add additional context in comments.

Comments

1

here you go: http://jsfiddle.net/8SEz2/2/

I added the attribute data-branch to the checkboxes

and here his the js:

$(".payMe").change(function(){
    var branch = $(this).attr('data-branch');        
    alert( $("input[data-branch='" + branch + "']:checked").length );
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.