Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check multiple checkbox selection using jquery

I am trying to find the easiest way to get the checkboxes that are selected.

Here's my script:

$(document).ready(function() {
    $("input[name='chkTextEffects']").change(function() {
        if ($("#cbSolid").is(':checked') == true) {
            alert('Solid');
        } else if ($("#cbOutline").is(':checked') == true) {
           alert('Outline');
        } else if ($("#cbSolid", "#cbOutline").is(':checked') == true) {
            alert('SolidOutline');
        } else if ($("#cbSolid", "#cbOutline").is(':checked') == false) {
            alert('No Effects'); 
        }
    });
});​

HTML:

   <input type="checkbox" name="chkTextEffects" id="cbSolid" value="Solid" />Solid
   <input type="checkbox" name="chkTextEffects" id="cbOutline" value="Outline" />Outline
   <input id="TextEffectsSelection" type="hidden" />

I'm not sure about this line if ($("#cbSolid", "#cbOutline").is(':checked') == true) or should I use bind to get that worked.

like image 397
coder Avatar asked Apr 23 '12 17:04

coder


People also ask

How do you checked multiple checkbox in jQuery?

$('#CheckAll'). change(function(){ if ($(this).is(":checked")) { $('. checkboxes'). each(function(){ $(this).

How do you check multiple checkbox is checked or not?

Since you're providing the same name attribute to all the checkboxes (from your PHP loop), you can use the selector input[name="city[]"] to target and find them all. But to find out how many specifically are checked, you can add the :checked selector. An alternative to this is using $('input[name="city[]"]').

How can I get multiple checkbox values?

Read Multiple Values from Selected CheckboxesUse the foreach() loop to iterate over every selected value of checkboxes and print on the user screen. <? php if(isset($_POST['submit'])){ if(! empty($_POST['checkArr'])){ foreach($_POST['checkArr'] as $checked){ echo $checked.

How do you check if all the checkboxes are checked in jQuery?

change(function(){ var a = $("input[type='checkbox']. abc"); if(a. length == a. filter(":checked").


Video Answer


2 Answers

Here is an example I created that demonstrates what I think you're attempting to achieve:

$('#getCheckboxesButton').live('click', function(event) {
    var checkboxValues = [];
    $('input[type="checkbox"]:checked').each(function(index, elem) {
        checkboxValues.push($(elem).val());
    });
    alert(checkboxValues.join(', '));
});

http://jsfiddle.net/qdvng/

Let me know if that helps. Its basically using the ':checked' jQuery selector to retrieve checkboxes that are checked, then iterating through their values and printing it out.

like image 145
SuperPomodoro Avatar answered Sep 23 '22 13:09

SuperPomodoro


You can use the :checked selector like this to get all checked checkboxes with the specified name:

$("input[name='chkTextEffects']:checked")
like image 35
Craig Avatar answered Sep 24 '22 13:09

Craig