Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is checkbox attribute in HTML?
3.
Usage of Checkbox in HTML
4.
How to Create a Checkbox in HTML?
4.1.
HTML
5.
HTML Checkboxes Labels
5.1.
HTML
6.
Form Attributes of HTML Checkbox
6.1.
HTML
7.
Checkbox vs Radio Buttons in HTML
8.
Frequently Asked Questions
8.1.
What is checkbox attribute in HTML?
8.2.
How to create a checkbox in HTML?
8.3.
How do I select a single checkbox in HTML?
8.4.
How do I add a checkbox to a table in HTML?
9.
Conclusion
Last Updated: Aug 2, 2024
Easy

HTML Checkbox

Author Nikunj Goel
0 upvote

Introduction

HTML checkboxes are a quintessential element of user interaction on web platforms. They provide a straightforward yet effective means for users to indicate their preferences, particularly in forms. Whether agreeing to terms and conditions, selecting from multiple options, or responding to binary yes/no queries, checkboxes facilitate a fluid user experience.

HTML Checkbox

In this blog, we will learn about the HTML checkbox and will explore its core concepts, real-world applications, and future aspects. 

What is checkbox attribute in HTML?

A checkbox is an HTML input element represented by <input type="checkbox">. It's used to create a box that users can click to toggle a selection. The type attribute is crucial, defining the checkbox behavior.

Usage of Checkbox in HTML

Checkboxes play a vital role in HTML forms, enabling users to make multiple selections among various options. When a user clicks a checkbox, it toggles between selected and unselected states. Checkboxes in HTML have several important uses, including:

  1. User Input and Form Submission: These are commonly used in HTML forms to allow users to select multiple options. The selected values can be submitted as part of a form, providing a way for users to make multiple selections.
  2. Settings and Preferences: These are often used in settings panels or user profiles to enable or disable certain features or preferences. Users can toggle checkboxes to indicate their choices, and the state of these checkboxes can be saved for future visits.
  3. Agree/Disagree Options: They can be used for binary choices, such as agreeing or disagreeing with terms and conditions. For example, a "Terms and Conditions" checkbox might be required to be checked before submitting a form.
  4. Filtering and Sorting: In data-driven applications, checkboxes can be used to filter or sort displayed content. Users can select checkboxes to show or hide specific items based on certain criteria.
  5. To-Do Lists and Task Management: They are commonly used in to-do lists or task management applications. Users can mark tasks as completed by checking the corresponding checkboxes.

How to Create a Checkbox in HTML?

Creating a checkbox involves using the <input> tag with type="checkbox". You can customize the appearance and behavior further by adding attributes like id, name, and value. This tag creates the checkbox element. Let us look at the example to understand.

  • HTML

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Checkbox Example</title>
</head>
<body>

<h2>Checkbox Example</h2>
<input type="checkbox" id="myCheckbox" name="myCheckbox" value="selected">

</body>
</html>

Output:

output

HTML Checkboxes Labels

To improve usability, associate labels with checkboxes. Use the <label> tag, providing a clickable text label for the associated checkbox. When users click the label, it toggles the checkbox state. Let us look at the example to understand checkbox labels.

  • HTML

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Checkbox Example</title>
</head>
<body>

<h2>Checkbox Example with Labels</h2>
<label for="checkbox1">Option 1</label>
<input type="checkbox" id="checkbox1" name="option1" value="selected">

<label for="checkbox2">Option 2</label>
<input type="checkbox" id="checkbox2" name="option2" value="selected">

<label for="checkbox3">Option 3</label>
<input type="checkbox" id="checkbox3" name="option3" value="selected">

</body>
</html>

Output:

output

Form Attributes of HTML Checkbox

Checkbox attributes like name and value are essential. The name attribute identifies the checkbox in the form, allowing server-side processing. The value attribute specifies the data sent to the server when the form is submitted. Let us look at the example to understand.

  • HTML

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Checkbox Example</title>
</head>
<body>

<h2>Checkbox Example</h2>

<form action="#">
<label for="myCheckbox">Select me:</label>
<input type="checkbox" id="myCheckbox" name="myCheckbox" value="selected">
</form>

</body>
</html>

Output:

output

Checkbox vs Radio Buttons in HTML

FeatureCheckboxRadio Button
Type <input type="checkbox"><input type="radio">
Selection LimitMultiple selections allowedExclusive single selection within a group
GroupingNot typically groupedGrouped using the name attribute
Use CaseSuitable for multiple, independent choicesIdeal for mutually exclusive choices
Default BehaviorCan exist independently or in groupsForm a group where only one can be selected
Markup<input type="checkbox"><input type="radio" name="group">
Example<input type="checkbox" id="option1"><input type="radio" name="group" id="option1">

Frequently Asked Questions

What is checkbox attribute in HTML?

The checkbox attribute in HTML is represented by <input type="checkbox">.

How to create a checkbox in HTML?

To create a checkbox in HTML, use the <input> element with the type attribute set to "checkbox."

How do I select a single checkbox in HTML?

To select a single checkbox in HTML, use the checked attribute within the <input> tag.

How do I add a checkbox to a table in HTML?

To add a checkbox to a table in HTML, insert a <input type="checkbox"> element within a table cell (<td>).

Conclusion

HTML checkboxes, with their simplicity and effectiveness, remain a cornerstone in web development for user interaction. Their ubiquitous presence in digital forms underlines their significance in capturing user preferences. As web development continues to evolve, so does the versatility and capability of HTML checkboxes, making them a timeless asset in the toolbox of web developers.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Happy Learning!

Live masterclass