Enabling and disabling checkboxes in HTML
This article explores the methods of enabling and disabling checkboxes in HTML.
Checkboxes in HTML
A checkbox is an interactive box that can be toggled to indicate yes or no. It is widely used in forms and dialog boxes.
Checkboxes are used when there are a number of options and the user is free to select any number of them, including zero. This means checking one does not automatically uncheck the others.
Checkboxes are mainly used when there are options that are not mutually exclusive. A small box will appear on the side of each option that can be toggled.
By default, the box is empty. An empty box indicates a negative, or that the user did not select the option.
When you click, a check mark appears in the box. A check mark means yes.
When you click again, the check mark disappears.
You can <input>
create a check box in HTML using the tag and set its type attribute to check box. You can <input>
write the item name after the tag.
For example, create an unordered list using the ul tag and set the style to list-style: none;
so that no bullets appear on the list items. Create four list items and <input type="checkbox">
create a checkbox for each list item using .
Code example:
<ul style="list-style: none;">
<li><input type="checkbox">Drink</li>
<li><input type="checkbox">Eat</li>
<li><input type="checkbox">Code</li>
<li><input type="checkbox">Repeat</li>
</ul>
Each list item has a toggle functionality in its respective checkbox by default.
Use the checked attribute to enable a checkbox by default in HTML
If you need a checkbox where one option is always checked by default, you can <input>
implement an attribute for the tag. The attribute to use is the checked attribute.
You just need to set the value of the property to selected to make it selected by default.
For example, for the Drink and Repeat options, add the attribute checked and set its value to “checked.” This ensures that the checkboxes for both values are checked by default.
Sample code:
<ul style="list-style: none;">
<li><input type="checkbox" checked="checked">Drink</li>
<li><input type="checkbox">Eat</li>
<li><input type="checkbox">Code</li>
<li><input type="checkbox" checked="checked">Repeat</li>
</ul>
Click to turn off the selected option at any time.
We can also use checked alone to get the same output.
Sample code:
<ul style="list-style: none;">
<li><input type="checkbox" checked>Drink</li>
<li><input type="checkbox">Eat</li>
<li><input type="checkbox">Code</li>
<li><input type="checkbox" checked>Repeat</li>
</ul>
Disable a checkbox by default using the disabled attribute in HTML
To disable a checkbox by default in HTML, you can use an attribute called disabled. Specifying the value of the disabled attribute as disabled will prevent the user from selecting the checkbox.
Consider the following example.
Sample code:
<ul style="list-style: none;">
<li><input type="checkbox" disabled="disabled">Drink</li>
<li><input type="checkbox">Eat</li>
<li><input type="checkbox">Code</li>
<li><input type="checkbox" disabled="disabled">Repeat</li>
</ul>
It is clear from the output that only two of the four options have toggle functionality. Eat and Code can be toggled to show tick marks, but Drink and Repeat cannot.
The options Drink and Repeat are disabled by default. When we click on the checkbox next to the Drink and Repeat option, the checkbox does not respond to our click.
This leaves only two of the four options to check.
The same result can be achieved by using disabled alone .
Sample code:
<ul style="list-style: none;">
<li><input type="checkbox" disabled>Drink</li>
<li><input type="checkbox">Eat</li>
<li><input type="checkbox">Code</li>
<li><input type="checkbox" disabled>Repeat</li>
</ul>
So, these are the ways to enable or disable a checkbox in HTML.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Creating a currency input in HTML
Publish Date:2025/04/19 Views:119 Category:HTML
-
In this article, users will learn to input convert a normal HTML field into a currency input field. We will use HTML and jQuery to achieve our goal. input Convert plain to currency input in HTML using JavaScript toFixed() method In HTML, th
HTML Hide Buttons and Show Them Using Onclick
Publish Date:2025/04/19 Views:178 Category:HTML
-
This article will show you several ways to hide HTML buttons and make them visible using the onclick event. Using the CSS display property to show hidden buttons in HTML We can first hide the HTML button by setting its display property to n
Right align button in HTML
Publish Date:2025/04/19 Views:136 Category:HTML
-
Alignment is the positioning of text or buttons in different places like right, left or center on an HTML web page. We can easily use the alignment properties to place our text, images or other elements anywhere on the web page. There may b
Readonly in HTML Select Tag
Publish Date:2025/04/19 Views:176 Category:HTML
-
TML, Hypertext Markup Language, is widely used to format websites. Also, CSS (Cascading Style Sheets) can be used to style websites. Below are some of the attributes used in HTML and CSS to control user and cursor behavior. select readonly
The role attribute in HTML
Publish Date:2025/04/19 Views:142 Category:HTML
-
In this article, we will introduce the HTML role attribute and its uses. Introduction to the role attribute in HTML The role attribute in HTML belongs to the WAI-ARIA (Web Accessibility Initiative - Accessible Rich Internet Applications) sp
Pagination when printing in HTML
Publish Date:2025/04/19 Views:147 Category:HTML
-
This article will introduce several ways to force page breaks when printing HTML content. Set the page-break-after property to always Inside @media print to Page Break in HTML The @media print rule of CSS lets us apply rules when printing H
Displaying Cookie-Based Images in HTML
Publish Date:2025/04/19 Views:95 Category:HTML
-
This article will introduce a method to display images in HTML format based on cookies set in PHP. Display images based on cookies set in HTML The problem statement is that we need to display a specific image based on a cookie passed by the
Creating a download link in HTML
Publish Date:2025/04/19 Views:158 Category:HTML
-
This article will introduce a method to create a download link in HTML. Use the download attribute to create download links in HTML We can create a download link using the download attribute inside the HTML anchor element. In the anchor tag
::before Selector in HTML
Publish Date:2025/04/19 Views:124 Category:HTML
-
This article will introduce CSS ::before pseudo-elements and explore their application in HTML. CSS ::before pseudo-element ::before The selector is a CSS pseudo-element that we can use to insert content before one or more selected elements