6

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.

2
  • Based on your demo, it looks like you are using checkboxes to mimic radio button behavior. Your if/else if statements seem to confirm that you are only looking for a single selected element. You probably want to use radio buttons instead. Commented Apr 23, 2012 at 17:37
  • @DanA-No,I want to get both the elements to be selected and sometimes may be one also.So based on that I need to get the checked one's. Commented Apr 23, 2012 at 17:38

4 Answers 4

9

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.

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

7 Comments

@SuperPomodoro-Yes,Exactly this is what I need.Cheer :)
Additionally, you can use the :not selector to retrieve the checkboxes that are not checked. jQuery selectors (and new CSS3 pseudo-selectors) allow for elegant DOM querying.
rhetorical question; but how did you get to the complete solution from this question LOL
@SuperPomodoro-Instead of button Click how could I get on selecting each individual checkbox.
@coder You've got the answer in your question. $("input[name='chkTextEffects']").change(function() {//do stuff}); Every time one of your checkboxes changes state, a change event is triggered.
|
4

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

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

Comments

1

JQUERY

$('input:checkbox:(:checked)').each( function() {
// your code here
})

http://api.jquery.com/checkbox-selector/

http://www.wiseguysonly.com/2010/01/15/select-and-unselect-all-checkboxes-with-jquery/

Comments

0

Why not do this:

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

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.