Check multiple checkboxes with jquery

I am trying to find the easiest way to get checked flags.

Here is 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 am not sure about this line if ($("#cbSolid", "#cbOutline").is(':checked') == true)or should use bindit to work.

+3
source share
4 answers

Here is an example that I created that demonstrates what I think you are trying 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 this helps. Its basically using the jQuery ': checked' selector to extract the checked checkboxes and then iterate over their values ​​and print. A.

+7
source

You can use : checked selector like this to set all checkboxes with the given name:

$("input[name='chkTextEffects']:checked")
+3
+1

Why not do it:

$("#checkboxId").attr("checked") == "checked";
0
source

All Articles