How to Set Checkboxes Readonly in HTML ? - GeeksforGeeks (2024)

Last Updated : 05 Jan, 2024

Summarize

Comments

Improve

In this article, we will learn whether it is possible to make checkboxes readonly in HTML or not. Checkboxes are commonly used to allow users to select multiple options from a set of choices. They are represented in HTML by the <input> tag with the type=”checkbox” attribute. It is not possible to set checkboxes to read-only using standard HTML attributes.

However, there are alternative approaches such as using the disabled attribute or CSS properties like pointer-events that can simulate the read-only behavior for checkboxes. By using these approaches, you can achieve the same effect as read-only checkboxes.

Table of Content

  • Using disabled option
  • Using onclick method
  • Using the pointer-events property

Using disabled option

This approach uses the disabled option with the input tag which represents 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: In this example we will see the the implementation 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:

How to Set Checkboxes Readonly in HTML ? - GeeksforGeeks (1)

Using onclick method

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: In this example we will see the implementation of how 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:

How to Set Checkboxes Readonly in HTML ? - GeeksforGeeks (2)

Using the pointer-events property

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: In this example we will see how 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:

How to Set Checkboxes Readonly in HTML ? - GeeksforGeeks (3)



Please Login to comment...

How to Set Checkboxes Readonly in HTML ? - GeeksforGeeks (2024)
Top Articles
7 Ways Being a Stay at Home Mom Saves Money ($17,000 a year!)
Real Estate - Mexico | Statista Market Forecast
Craigslist San Francisco Bay
Pinellas County Jail Mugshots 2023
Cad Calls Meriden Ct
PRISMA Technik 7-10 Baden-Württemberg
A Complete Guide To Major Scales
Jonathan Freeman : "Double homicide in Rowan County leads to arrest" - Bgrnd Search
Computer Repair Tryon North Carolina
Craigslist Vermillion South Dakota
CHESAPEAKE WV :: Topix, Craigslist Replacement
Palace Pizza Joplin
Edible Arrangements Keller
Mens Standard 7 Inch Printed Chappy Swim Trunks, Sardines Peachy
Tcgplayer Store
2016 Ford Fusion Belt Diagram
Connect U Of M Dearborn
Simplify: r^4+r^3-7r^2-r+6=0 Tiger Algebra Solver
Boston Gang Map
Apply for a credit card
Satisfactory: How to Make Efficient Factories (Tips, Tricks, & Strategies)
Finalize Teams Yahoo Fantasy Football
Noaa Duluth Mn
Understanding Gestalt Principles: Definition and Examples
Foolproof Module 6 Test Answers
Plost Dental
Soul Eater Resonance Wavelength Tier List
Gen 50 Kjv
Cal State Fullerton Titan Online
Effingham Daily News Police Report
Anesthesia Simstat Answers
Sam's Club Near Wisconsin Dells
Acuity Eye Group - La Quinta Photos
Ultra Clear Epoxy Instructions
Today's Final Jeopardy Clue
Rochester Ny Missed Connections
Rage Of Harrogath Bugged
Saybyebugs At Walmart
South Bend Tribune Online
Armageddon Time Showtimes Near Cmx Daytona 12
Florida Lottery Claim Appointment
Grizzly Expiration Date Chart 2023
Craigslist St Helens
Human Resources / Payroll Information
Laura Houston Wbap
Heat Wave and Summer Temperature Data for Oklahoma City, Oklahoma
Who Is Nina Yankovic? Daughter of Musician Weird Al Yankovic
Appsanywhere Mst
Lsreg Att
When Is The First Cold Front In Florida 2022
Noaa Duluth Mn
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 5886

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.