Check if an HTML Checkbox is checked using Javascript
While processing forms in web development, it’s a common requirement to be able to tell if a checkbox has been checked or not.
It’s not immediately obvious though. Let’s dispell this and provide some ways to check if a checkbox is checked
using Javascript.
First, we need an HTML checkbox to play around with.
|
|
Using Javascript
Using regular Javascript, we can tell if it is checked by using the following Javascript:
|
|
You can also use a Query Selector Pattern
to tell if a value matches as follows:
|
|
Using jQuery
jQuery comes with quite a nice way to do this using the is()
method.