jQuery tip: How to check if checkbox were checked?

1. The best and most performant is to use the DOM property directly, as in this.checked inside an event handler when this references the element that was clicked.

Example:


jQuery("#checkbox").click(function(){
   if(this.checked) { .... }
});

2. The $(this).is(":checked") also does the same job but not as fast as solution one. Though it can be used to specifically target an element and additionally it works with all jQuery version

if jQuery("#checkbox").is(":checked") { .. }

3. In code that uses jQuery 1.6 or newer, the new method $(this).prop("checked") retrieves the same value as this.checked and is relatively fast

if jQuery("#checkbox").prop("checked") { .. }
Advertisements

About Jones Lee

Nothing much about me..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: