Monday, December 29, 2008

How to check if checkbox is checked using jQuery

Note: If you are new to jQuery, you might find our short jQuery beginner tutorials useful.

While working on my previous project I needed a way to check if the certain checkbox is checked or not. jQuery is a great library but I had a hard time identifying if any particular checkbox was checked. So here is the way to do just that.

All you need to do is to check checked attribute of checkbox HTML tag:

// First way 
$('#checkBox').attr('checked'); 

// Second way 
$('#edit-checkbox-id').is(':checked'); 

// Third way for jQuery 1.2
$("input[@type=checkbox][@checked]").each( 
    function() { 
       // Insert code here 
    } 
);
// Third way == UPDATE jQuery 1.3
$("input[type=checkbox][checked]").each( 
    function() { 
       // Insert code here 
    } 
);

// In his comment Andy mentions that the 3rd method
// does not work in Firefox 3.5.2 & jQuery 1.3.2

First two methods return true or false. True if that particular checkbox was checked or false if it is not checked. The third method iterates though all checkboxes with checked attribute defined and performs some action.

See the latest jQuery tips. They are short, very short!

Want to learn how to check if jQuery.js is loaded into the current document? Also learn how to work with jQuery 1.3's new Event object.


19 comments:

Anonymous said...

Thanks. helped me out. I figured .checked would work.

Anonymous said...

It works. Thank's :)

Ciupaz said...

Hello,
I'm trying this script to disable one checkbox when another checkbox is checked by the user, but doesn't work.


$(document).ready(function() {

$('#checkbox1').is(':checked')
{
alert("OK");
$("#checkbox2").attr("disabled", true);
});

What's the problem in it?

Thanks a lot.

Luis

Ryan said...

I figured the first two out a while ago but do you know if any one way is better or worse than the others?

Uzbekjon said...

@Ryan, I just run performance tests using this method on 1000 checkboxes with id's and both (1&2) methods gave almost the same results of 175/180 ms. So it comes to your personal preference. So for me .is(':checked'); looks more readable :)

@Ciupaz, the first two methods return boolean values true/false. So you need to use if statement to check if first checkbox is checked like this:

$(document).ready(function() {
if($('#checkbox1').is(':checked')){
alert("OK");
$("#checkbox2").attr("disabled", 'disabled');
}
});

AOM said...

thanks,it work for me.

Rosdi said...

thanks a bunch! saved me a lot of time.

Andy said...

We have been using method 3 but it stopped working for us with Firefox somewhere around version 3.5.2. We are using JQuery 1.3.2.

In place of:
$("input[type=checkbox][checked]").each(
etc.

We were using:
$('input[type=checkbox][checked]').each(function(){
etc.

That still works with IE7 (7.05...) but not in FF3.5.2 or in Safari4.

I've tried various options, starting with '$("input[type=checkbox][checked]").each(' without success.

Finally the following works:

$("input[type='checkbox']:checked").each(function(){

This JS is within dynamically loaded HTML, generated from a Velocity template, which might contribute to the problem and certainly made debugging harder because FF and Firebug don't support debugging of such JS (as per http://code.google.com/p/fbug/issues/detail?id=1774) and the workaround for that (http://stackoverflow.com/questions/858779/making-firebug-break-inside-dynamically-loaded-javascript) did not work for me.

The equivalent for radio-buttons also stopped working - but bizarrely not in all cases.

I guess the key difference is between ][checked] and ]:checked

HSIRIG said...

thanks

Andy said...

'[checked]' is no longer working for us with FireFox 3.5.3 and JQuery 1.3.2.

':checked' is working.

Fellipe Brito said...

Great! I'll translate and put on my blog too!

echavanon said...

Thanx for the comment about FF 3.5.* & jQuery 1.3.2. It solved my brand new issue !

Sedat Kumcu said...

Thanks for this useful article. Good works.

Anonymous said...

good but not proper example give full example of newly intersted persons

Thobib said...

It works. Thank's :D

JM-DG said...

Thanks it helped me a lot and saved me time...

Abhijeet said...

its not working for me. help.
$(document).ready(function() {
if($('.param').attr('checked')){
$('#tickbox1').hide();
}
});

Anonymous said...

thank you.. it works fine

Grafik said...

Works fine,

Abhijeet, maybe you should pass a ID, not a class:

if($('.param')....

Post a Comment