Ways to enable users to use the keyboard tab to navigate and choose hidden checkboxes with labels that are visible

I am currently configuring "card" style checkboxes where there are three hidden checkboxes (opacity: 0) with corresponding labels displayed as boxes with backgrounds.

Clicking on the label selects the checkbox and changes its label to a blue background. Everything is functioning properly, except I noticed that I am unable to tab through the labels and select their respective checkboxes when the labels themselves are focused by pressing the space bar.

The checkboxes can be tabbed to and selected using the spacebar, but not when the label is in focus... it's like an inconsistency between the labels.

Note: Adding a tabindex="0" to the labels enables them to be focused on and highlighted.

Is there a way to achieve this without JavaScript? I am designing the UI for an Angular application, although my knowledge of Angular is limited.

I understand that different browsers may have inconsistencies in tabbing and input selection (I am working in Chrome), so I intend to consider that as well. Thank you.

/* Unchecked labels are red */
label {
  display: inline-block;  
  width: 100px;
  height: 50px;
  background-color: tomato;
}

/* Invisible checkboxes */
input[type="checkbox"] {
  opacity: 0;    
}

/* If checkbox is checked, blue background */
input[type="checkbox"]:checked + label {
  background-color: blue;
}
<input type="checkbox" id="1">
<label for="1" tabindex="0"></label>

<input type="checkbox" id="2">
<label for="2" tabindex="0"></label>

<input type="checkbox" id="3">
<label for="3" tabindex="0"></label>

Answer №1

Provided by @dacre-denny, this innovative response addresses the original question. However, it's important to note that both the initial code and the answer still have significant accessibility issues. Despite the fact that the accessibility tag was not originally mentioned in the post, I feel compelled to comment on this aspect.

The solution proposed involved adjusting the tabindex for the <label> elements to values of 0, 1, and 2. It's essential to avoid using tabindex values higher than 0.

According to the tabindex specification, there are two notable warnings:

Assigning a positive value to tabindex to indicate an element's position in the focus navigation sequence can lead to errors as it affects all focusable elements. Thus, this practice is not recommended.

Additionally,

Authors should only enable focus on elements if they function as interactive controls or widgets.

A <label> element does not serve as an interactive component and therefore should not possess a tabindex. If you desire the label to be interactive, incorporating click and keypress handlers along with assigning a suitable role is necessary for screen reader users to properly identify the element.

In my view, eliminating tabindex entirely from the <label> elements and allowing natural keyboard focus to reach the "hidden" checkboxes would ensure proper functionality. With @dacre-denny's styles, the red block will exhibit a focus indicator when the checkbox is selected, enabling users to utilize the space key to choose the hidden checkbox and witness the color change.

(I tested this by removing tabindex from the <label> elements using the browser's code inspector in the "code snippet," successfully navigating through the blocks and selecting them with the space key.)

Answer №2

If I understand correctly, what you're looking for can be accomplished using the :focus selector. You can implement this CSS code:

input[type="checkbox"]:focus + label {
  border:2px solid red;
}

With this code, a red border will appear around the label when the corresponding checkbox is in focus. This helps users keep track of the focused checkbox as they navigate through the collection.

It's also recommended to adjust the tabindex values of your checkbox inputs for consistent tab navigation. Here's an example:

/* Red labels for unchecked checkboxes */
label {
  display: inline-block;  
  width: 100px;
  height: 50px;
  background-color: tomato;
}

/* Hide the checkboxes */
input[type="checkbox"] {
  opacity: 0;    
}

/* Blue background for checked checkboxes */
input[type="checkbox"]:checked + label {
  background-color: blue;
}

/* Additional styling for focusing on checkboxes */
input[type="checkbox"]:focus + label {
  border:2px solid red;
}
<input type="checkbox" id="1">
<label for="1" tabindex="0"></label>

<input type="checkbox" id="2">
<label for="2" tabindex="1"></label>

<input type="checkbox" id="3">
<label for="3" tabindex="2"></label>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A guide on how to navigate the parent window from a child window

When using my application, I have a function that opens a new window like this: window.open("www.example.com"); Within the new window, there is a button. Upon clicking this button, the new window should close and then redirect the parent window to a diff ...

Secure Social Security Number (SSN) Input Field showing only the final four digits | includes option to show/hide

I need to show only the last four digits of the SSN and mask the rest with asterisks, like: ****-**-7654 Additionally, I want to implement a toggle functionality where users can reveal or hide the masked characters behind the asterisks. <in ...

What are the steps to create two adaptable blocks and one immovable block?

Check out this HTML code snippet: <div style="border: 1px solid #000; width: 700px; line-height: 38px; display: block"> <div style="margin-left:10px;width: 222px; float: left; margin-right: 10px;"> Enter your question </div& ...

I'm having trouble keeping items aligned on my navigation bar right after a form. Can anyone help me with this?

I'm having trouble fitting a navbar with these elements: My brand logo A lengthy search bar and submit button filling the entire height of the navbar. Space at the end of #2 for another button of equal height. ... all in one line without collapsing ...

Include a new variable in a JavaScript email template

Is there a way to include the variable bob in the body of an email? var bob; function sendMail() { var link = "mailto:YourEmailHere" + "?cc=" + "&subject=App Build Link Buit With MWFPRO's App Build Tool" + "&body=Hi ...

JavaScript Number Conversion with parseInt()

Struggling to execute a calculation that fills a text box based on an onblur event. Unfortunately, I keep getting NaN as the result. Since I am not very familiar with JavaScript, any guidance will be greatly appreciated. Please note that I am mainly focus ...

Utilizing the options object within JavaScript functions: A step-by-step guide

My current task involves gathering input as an argument along with some objects. function display(parameter) { var text = parameter.text; var element = parameter.element; document.getElementById(element).innerHTML = text; } As part of this task, I a ...

Using Jquery ajax to make real-time updates to a MySQL database

In my application, I have a MySQL table called 'company' which includes columns such as companyid and activation_code. I am displaying the values from this table in an HTML table on a page named comapproval.php. Each row in the HTML table contain ...

Incorporate dual repeated background images within one div element

I am struggling to apply two repeating background images in the X direction for my div. Is there a way to achieve this? I want to divide my single div into two equal parts, using the first image with background repeat-x for the first part and the second ...

Is there a way to apply -webkit-line-clamp to this JavaScript content using CSS?

i have a random-posts script for my blogger website <div class="noop-random-posts"><script type="text/javascript"> var randarray = new Array(); var l=0; var flag; var numofpost=10; function nooprandomposts(json){ var total = ...

What is the best way to transform URL images on a webpage into media library images in WordPress?

Having trouble converting HTML Static Sites to WordPress because images are hosted through HTML SRC Code. After conversion, no one can see the images in visual format on pages during editing. Is there a way to convert all image links inside pages to media ...

Steps for utilizing an `<a>` link tag to submit a form in Angular 4

Is there a way to retrieve the selected option in this form from the other side when clicking a link? <form (ngSubmit)="onSubmit(x)"> <input type="radio" id="radioset3" name="radioset" [checked]="x==0"> <input type="radio" id="radio ...

Is there a way to transform JSON into HTML with PHP?

I am attempting to reverse engineer the function referenced in this discussion: [Post][1] [1]: https://stackoverflow.com/a/23427469/19867306 @scozy demonstrated a function for converting HTML text to a JSON model. Now, I need to convert the same JSON mode ...

Properly Adding an External jQuery File in HTML Using jQuery

Seeking assistance as a newcomer to JS and programming in general. Currently working on a website where each page has its own HTML / PHP file, with jQuery and global JS functions included in the footer via a separate includes file "footer.php". Everything ...

Is it necessary to download and install plotly if I am using the cdn in my HTML file?

I'm currently developing an online application using Flask. The user input is collected with d3.js and sent to app.py, where it is used for an API call to retrieve the necessary data. This data is then returned in JSON format to JavaScript for renderi ...

Using jQuery to toggle visibility of various elements without needing specific identifiers

I have coded a HTML and JS snippet for displaying and hiding a div element, which is currently functioning correctly. However, I want to be able to use multiple boxes on a single page and I need a way to determine which box-header was clicked. Is there a ...

Is there a way to utilize the function body onload in jQuery?

I have some code that needs to be fixed. Currently, when I see <body onload="start()" onresize="resize()" onorientationchange="resize()">, I want the following code snippet to work: $("button").click(function(){ $("body").onload = start; or win ...

What is the process for building a series of HTML elements using jQuery?

Is it possible to create a moving conveyor belt of html elements (such as divs) using jQuery, and if so, how can this be done? For example: (as shown above, rectangles move from left to right; new ones are added and the last one is removed) And then: [ ...

Transforming a Multi-Dimensional Array into an HTML Table

Experimenting with a new idea. I hope the solution is straightforward; I just can't seem to figure it out. Imagine I have an array like this: var items = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]; My goal is to rearrange the data so that it looks like t ...

Create a responsive layout of inline-block elements that adjusts to resizing

I am currently focusing on developing the website found at: The progress so far includes successfully creating a list of div.project-item elements that adjust the number of columns based on window resizing due to the use of inline-blocks. My next goal is ...