HTML Input Type Checkbox Field

A checkbox field in HTML is a square-shaped box that users can click inside the form. You can create an HTML input type checkbox field using the syntax <input type="checkbox"> in HTML. You can check the below-given methods and examples to create an input type checkbox.

HTML Input Type Checkbox Field Using <input type=”checkbox”>

The checkbox field is useful when you want to allow users to select one or more options from the given number of choices. You can get user’s input data such as hobbies, technical skills, and other useful details using the input type checkbox field in a form. You can create this using the syntax given below.

Syntax
<input type=”checkbox”>

The above syntax showing only the type attribute of the input checkbox. However, there are many other attributes available for it that you will learn further in this tutorial.

Add Label For HTML Input Type Checkbox Field

If you want to get the required value for the input checkbox field, you have to show the text with it. The text comes after the checkbox with each option in a form. Use the <label> tag after the checkbox to show the text as given below.

Test it Live

Output

HTML input type checkbox element with label

The above example shows the input checkbox with the text showing after it. The text defines the purpose of the checkbox in the form.

Input Type Text Field in HTML Form

Checkbox Field in HTML Using <input type=”checkbox”>

You can use the checkbox field in HTML using <input type=”checkbox”> to display multiple choices to users to select. Let’s see the simple example to create checkboxes using the above syntax.

Example

Test it Live

Output

HTML input type checkbox element

The above example contains four simple checkboxes for users to select single or multiple choices of programming skills. It also contains the name attribute that you need to give same to allow users to select and id of the input textbox.

Make Checkbox Field Required in HTML

When you want to get the user’s input for the checkbox field, you need to make it required in a form. If the checkbox field is required, it will not allow users to submit forms without selecting a choice.

You can easily make the input checkbox field required by using the jQuery script as given below.

Test it Live

Output

HTML input type checkbox element make required

The above example showing three input type checkbox fields. When you click the button given above, the jQuery script will not allow users to submit the form. The users must have to select single or multiple checkboxes to submit the form.

If you do not select a choice, you will get an alert message to hint users to choose your choice for submitting the form.

Some Other Attributes of <input type=”checkbox”>

Below are the useful attributes you can use with the checkbox.

Attribute Name Description
value
  • Specify a default value for the input type checkbox which is useful to get the purpose of the checkbox in a form.
  • You can enter a text or number as the value of this attribute.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.