In this article, we are going to find out whether checkboxes can only be readonly or not. In HTML, checkboxes let users choose, multiple options from a number of choices or options. The ‘<input>‘ tag with the type=”checkbox” property serves as their representation. Checkboxes cannot be set to readonly using standard HTML attributes. However, alternative approaches like using the disabled attribute or CSS properties like pointer-events can mimic the read-only behavior for checkboxes. But you can achieve it by using these approaches.
Approach 1: This approach is using the disabled option with the input tag which is representing the checkboxes. When the disabled option is added to the input tag, the checkbox is essentially disabled so you can view the checkbox but cannot interact with it. You can also use the ‘checked’ option and get the read-only checkbox which is already checked but cannot be unchecked.
Syntax:
<input name=".." type="checkbox" disabled value="1" />
Used Checkbox options:
- disabled: This option disables the checkbox from interacting with any event and shows its initial checked or unchecked version.
Example: This code example shows the implementation of the first approach to make the checkboxes readonly using the disabled option:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>checkboxes be set to readonly</title>
</head>
<body>
<h1 style="color: green; margin: 2rem">
GeeksforGeeks
</h1>
<h3 style="margin: 2rem; margin-top: -1.5rem">
Can checkboxes be set to readonly?
</h3>
<div style="margin: 2rem;">
<input name="chkBox_1" type="checkbox"
disabled value="1" />
<label for="chkBox_1">
Disabled unchecked option
</label>
</div>
<div style="margin: 2rem;">
<input name="chkBox_2" type="checkbox"
disabled checked value="1" />
<label for="chkBox_2">
Disabled checked option
</label>
</div>
</body>
</html>
|
Output:
Approach 2: This approach is using the onclick method with the input tag which is representing the checkboxes. When the on-click method is added to the input tag, and it is set to a “return false” value the checkbox is essentially disabled so you can view the checkbox but cannot interact with it.
Syntax:
<input type="checkbox" onclick="return false"/>
Used method:
- onclick: This event handler you to specify a function to be executed when the click event occurs on the element.
Example: This code example shows the implementation of the second approach to make the checkboxes read-only using the onclick method:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>checkbox to be set readonly</title>
</head>
<body>
<h1 style="color: green; margin: 2rem">
GeeksforGeeks
</h1>
<h3 style="margin: 2rem; margin-top: -1.5rem">
Can checkboxes be set to readonly?
</h3>
<div style="margin: 2rem;">
<input name="chkBox_1" type="checkbox"
onclick="return false" />
<label for="chkBox_1">
Disabled unchecked option with Onclick method
</label>
</div>
<div style="margin: 2rem;">
<input name="chkBox_2" type="checkbox"
onclick="return false" checked />
<label for="chkBox_2">
Disabled checked option with onclick method
</label>
</div>
</body>
</html>
|
Output:
.gif)
Approach 3: This approach is using the pointer-events CSS property to the input tag which is representing the checkboxes. When this CSS property is given as design to the input tag, the checkbox is essentially read-only so you can view the checkbox but cannot interact with it.
Syntax:
input[type="checkbox"]{
pointer-events: none;
}
Used Property:
- pointer-events: This property is used to control how an element responds to user interactions with a pointing shows read device, determining whether it should receive pointer events or not.
Example: This code example shows read–only the implementation of the third approach to make the checkboxes readonly using the pointer-events property:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>checkbox to be set readonly</title>
<style>
input[type="checkbox"] {
pointer-events: none;
}
.head1 {
color: green;
margin: 2rem;
}
.head2 {
margin: 2rem;
margin-top: -1.5rem;
}
.div1 {
margin-top: 2rem;
}
.div2 {
margin-top: 2rem;
}
</style>
</head>
<body>
<h1 class="head1">
GeeksforGeeks
</h1>
<h3 class="head2">
Can checkboxes be set to readonly?
</h3>
<div class="div1">
<input name="chkBox_1" type="checkbox" />
<label for="chkBox_1">
Disabled unchecked option with
Pointer-events
</label>
</div>
<div class="div1">
<input name="chkBox_2" type="checkbox"
checked />
<label for="chkBox_2">
Disabled checked option with
Pointer-events
</label>
</div>
</body>
</html>
|
Output:
.gif)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
25 Jul, 2023
Like Article
Save Article