The JavaScript Checkbox Checked Sets or Returns the Value of a Checkbox

javascriptsplitAren’t you glad that website forms include elements like checkboxes and radio buttons? Checkboxes and radio buttons allow you to quickly and simply select your preferences without having to type lengthy answers to certain questions. These elements are extremely handy and also fairly easy to create in your website forms using JavaScript. To get started with JavaScript, checked sets, and returns, keep reading to harness the power of JavaScript in your web pages.

JavaScript is probably one of the most popular scripting languages used to create interactive websites. JavaScript is a powerful object oriented, client-side, scripting language that offers fairly simple syntax to allow programmers to create dynamic websites that include asynchronous communication to communicate with users.

This tutorial will show you how to create checkboxes for your website and how to set the checkbox options for the checkbox. To follow the elements within this tutorial, I highly recommend that you understand the basics of JavaScript. A course like Javascript for Beginners will teach you what you need to know to follow this tutorial.

Radio Buttons or checkboxes?

Radio buttons and checkboxes allow users to select values with a click of the mouse. Radio buttons appear as a small circular option on a page. Checkboxes appear as a small square on a page.

Checkboxes are different from radio buttons because radio buttons allow users to select an option from various options, thus allowing the user to select a choice based on an either/or option whereas checkboxes allow a user to select more than one option at a time.

Getting to know the Checkbox object in JavaScript

As mentioned above, JavaScript is an object oriented programming language and as an object oriented language, the language is based on the use and manipulation of various objects. The checkbox object is one of the objects you can use within your JavaScript.

The checkbox object allows you to create checkboxes within your HTML forms. Checkboxes allow the user to select one or more options from a limited number of choices. The checkbox allows you to search for elements within the checkbox as elements of an array. You can also search for elements of a checkbox using the documents.getElementById() method.

The Checkbox object properties include:

  • checked – the checked properties sets or returns the state of the checkbox

  • defaultChecked – this property returns the default value of the checked attribute

  • disabled – this property returns whether the checkbox object is disabled or not

  • name – used to set the name of the object or return the name

  • value – sets the value of the object or returns the value

How to use the Checkbox object to create a checkbox in your form

Now that you know the properties of the checkbox object you can use the object in your JavaScript to create checkboxes for your web pages.

For the first example we will create a form that contains a checkbox. We will use the checked property of the checkbox to see whether the user has selected the box or not using the onClick event handler in JavaScript.

Here is the sample code you could use:


The above code creates a page with the following elements:

When the user clicks the checkbox, the onClick event runs the function to determine if the checkbox is clicked on not.

The first time the user clicks it, the output will look like this:

The second time the user selects the checkbox next to the select option, the check mark will be removed so the output will look like this:

Example of using Checkbox checked with multiple checkboxes

Our next code example will show you how to use the checkbox checked property with multiple checkboxes to confirm a client’s order.

Here is the code we will use for this example:


The form for the above code looks like this:

The code creates four checkboxes and assigns a unique value to each of them. To learn JavaScript fundamentals you can sign up for a course at udemy today.

The code also creates a function that checks the “checked” status of each checkbox to see if the user has selected the checkbox or not. If the checkbox checked status is true then the function creates an alert for that item. Assuming the client selects “Coffee” and “Milk” by selecting the two checkboxes next to those items the user will receive the following alerts:

JavaScript checkbox checked object and property

As you can see from the above examples, including checkboxes in your web pages is easy. The checked property allows you to set the checkbox initial value or return the value if the user has selected the checkbox or not.

For more advanced programming tutorials, why not sign up for the JavaScript with BackboneJS and Bootstrap CSS – Advanced course today from udemy and take your JavaScript skills to the next level.