1

Below is my structure.

<span class="mrQuestionTable">                                            
    <span id="Cell.0.0">
        <input type="checkbox" name="_QC05_C__1" id="_Q0_C0" class="mrMultiple" value="__1"/> 
        <span class="mrMultipleText" style="">Mobile-related fraud (Fraud via mobile channels)</span>
    </span>

    <span id="Cell.0.1">
        <input type="checkbox" name="_QC05_C__2" id="_Q0_C1" class="mrMultiple" value="__2"/>
        <span class="mrMultipleText" style="">Cost (managing, implementation related costs)</span>
    </span>
</span>

I used the below code to count the length. Once i hit the checkbox it shows me length of 0. Instead it should show 1. Please let me know why this is happening?

alert($('.mrMultiple:checkbox:checked').length);  
1
  • TRy this alert($('.mrMultiple:checked').length); Commented Sep 4, 2017 at 4:15

4 Answers 4

1

Try this:

$('.mrMultiple').on('change', function() {

  alert($('.mrMultiple:checked').length);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="mrQuestionTable">                                            
<span id="Cell.0.0">
<input type="checkbox" name="_QC05_C__1" id="_Q0_C0" class="mrMultiple"
value="__1"/>    
<span class="mrMultipleText" style="">Mobile-related fraud (Fraud via mobile 
channels)</span>
</span>
<span id="Cell.0.1">
<input type="checkbox" name="_QC05_C__2" id="_Q0_C1" class="mrMultiple"  
value="__2"/>
<span class="mrMultipleText" style="">Cost (managing, implementation 
related costs)</span>
</span>
</span>

Sign up to request clarification or add additional context in comments.

2 Comments

I am checking this on click. but my code is not working. Below is my code :- $('.mrQuestionTable .mrMultiple').click(function(){ alert($('.mrMultiple:checkbox:checked').length); }; However, code you provided is working.
You have a syntax error (missing a closing )) - otherwise your code would work.
1

I believe you want to check the length of the checkboxes only after a checkbox is clicked on. Otherwise the alert function will just run when the page loads and nothing is checked.

$(".mrMultiple").on("click", function() 
   alert($('.mrMultiple:checked').length);
}

2 Comments

Yes, i am checking this on click. but my code is not working. Below is my code :- $('.mrQuestionTable .mrMultiple').click(function(){ alert($('.mrMultiple:checkbox:checked').length); };
If it is not working you may need to wrap it in a document ready function.
1

To count number of checkbox present in the page

var c=$('input[type=checkbox]').length;

To count number of checkboxes checked in the page

var cc=$('input[type=checkbox]:checked').length

Comments

1

You need to put it in a click function or it will only ever run once no matter how many times you click.

$(document).ready(function() {
  $("input").on("click", function() {
    console.log($('.mrMultiple:checkbox:checked').length);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span class="mrQuestionTable">                                            
  <span id="Cell.0.0">
    <input type="checkbox" name="_QC05_C__1" id="_Q0_C0" class="mrMultiple"
    value="__1"/>    
    <span class="mrMultipleText" style="">Mobile-related fraud (Fraud via mobile 
    channels)</span>
</span>
<span id="Cell.0.1">
    <input type="checkbox" name="_QC05_C__2" id="_Q0_C1" class="mrMultiple"  
    value="__2"/>
    <span class="mrMultipleText" style="">Cost (managing, implementation 
    related costs)</span>
</span>
</span>

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.