Check if Checkbox is Checked

Say that 10 times fast =).

Find out if a single checkbox is checked or not, returns true or false:

$('#checkBox').attr('checked');

Find all checked checkboxes:

$('input[type=checkbox]:checked');

Comments

  1. User Avatar
    LuK
    Permalink to comment#

    $(“input[type=’checkbox’]:checked”).each(function(){

    // your code

    });

  2. User Avatar
    gabBerg
    Permalink to comment#

    What if a radio button is selected?

  3. User Avatar
    Rupam
    Permalink to comment#

    The website is horrible in IE6. At least the design for the page should be made differently to work in IE6

    • User Avatar
      mahen23
      Permalink to comment#

      Why are you still using IE6?

    • User Avatar
      Ivana Setiawan
      Permalink to comment#

      Agree with mahen23.

      Also, I think css-tricks’ viewers are mostly web developers – In this case, (I hope) they don’t use IE6 as their default browser..

    • User Avatar
      james
      Permalink to comment#

      Old School browser troll is Old School.

  4. User Avatar
    Hasmi
    Permalink to comment#

    var val=$(“input:checked”).length
    if(val>0){//At least one checked

    }

    if(val<0){//Nothing checked

    }

  5. User Avatar
    Summi
    Permalink to comment#

    Appreciate but you should upgrade the IE version.

  6. User Avatar
    Pranjay
    Permalink to comment#

    Another way to do this is $(element).prop(‘checked’) [ http://api.jquery.com/prop/ ]. This would always return a true or false value similar to what Wouter has suggested in is() method. $(‘#checkBox’).attr(‘checked’) however, will only return true when the box is checked and will return ‘undefined’ when unchecked.

  7. User Avatar
    check it
    Permalink to comment#

    Also, I opine css-tricks’ audience are mostly web developers – In this containerful, (I comedian) they don’t use IE6 as their nonpayment browser.

  8. User Avatar
    Stamatov
    Permalink to comment#

    $(‘#checkBox’)[0].attr(‘checked’);
    better add [0] to this code. I do this coz id is unique and there should be only one element with this id per page.
    Also useful if you want to checked/unchecked checkbox or radio button via jquery
    $(‘#checkBox’)[0].attr(‘checked’,true);
    $(‘#checkBox’)[0].attr(‘checked’,false);

    cheers!

    • User Avatar
      Chris Coyier
      Permalink to comment#

      Careful here, adding [0] to the end of that jQuery object will make it stop being a jQuery object and just a regular pointer thing. attr() will no long work on it.

  9. User Avatar
    Stamatov
    Permalink to comment#

    very true, my mistake.
    btw after small research i found that it will be better to use .prop() function about this case.

  10. User Avatar
    Adam
    Permalink to comment#

    Is there a reason you can’t use: $(‘#checkBox:checked’);

  11. User Avatar
    Adam
    Permalink to comment#

    On a separate but related note, has anyone noticed or explain why CSS and JQuery handle :checked differently? Where CSS looks at the latest state of a set of radios, jQuery only seems to see what is in the dom on page load. I created a jsfiddle to illustrate… http://jsfiddle.net/cssguru/8rDXd/

  12. User Avatar
    vinay
    Permalink to comment#

    This really nice article . have a look of this also
    http://dotnetpools.com/Article/ArticleDetiail/?articleId=199&title=Code-to-check-if-checkbox-is-checked-using-jQuery-|-Validate-checkbox-is-checked-or-un-checked

  13. User Avatar
    soncu
    Permalink to comment#

    Very good explanation

  14. User Avatar
    wishn4fishn

    This is great… I was looking for a way to do this but then I still need to figure out who to finish from there. Find all the boxes checked, sum the value of each one checked and then carry on to the next stage of the filtering process…. So many things.

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag