E-Learning

Learn JavaScript – How to Verify if a Checkbox is Checked in jQuery?

 
If you are using jQuery and want to find out if a particular checkbox is checked, inside jQuery there are some fairly straightforward ways to do this. In this article, we will explore some of them, along with some examples.
 

jQuery – checking a checkbox with is ( ‘:checked’)

The first way to check this is by using the function is () in jQuery. It checks if the argument or set of arguments that you stated satisfies the given condition. If so, it returns “true”. If not, it returns “false”.

To use is (), we first need to choose the element and then do the check using the selector :checked, that works with several elements, including checkboxes. Example below:

 

jQuery – checking a checkbox with prop ( ‘:checked’)

Before  jQuery 1.6 the function attr () was used to obtain both attributes as the properties of an element. This creates some confusion, so much so that after jQuery 1.6 a new function, called prop () was designed to verify the property of the element analyzed.

A short explanation about the difference between attributes and properties. Simply put, the attribute can be considered the value declared in the code, as the property is the value when the check is done. If the user changes this value, such happens with a checkbox, for example, what you want is the property of this element. Example using prop ():

Do you know other ways to check if a checkbox is checked using jQuery? Share with us in the comments area at the bottom!

If you want to explore more about jQuery, visit our section of videos! Below are some examples:

You can also follow some of the broadcasters who program in jQuery, as below:

 

checkbox jQuery JavaScript programmingkhaleelsyed

checkbox jQuery JavaScript programmingtoferj

Another cool way to find out interesting things about jQuery is to access our jQuery project page!

Avatar
About author

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.