Designed radio buttons failing to activate when focused using a keyboard

I'm facing an issue with radio input buttons that I've dynamically added to a div using jQuery. They function properly when clicked with a mouse, but do not get activated when using the keyboard tab-focus state.

NOTE: To style the radio buttons as rectangles holding label names, I have used visibility: hidden for the radio button and opacity: 0 for the inputs at different times. Despite adding aria-hidden attribute and tabindex = "0" on the labels to overcome event listener quirks on append, it does not seem to work correctly with the input elements.

I hope this code snippet provides enough context to understand the situation, but feel free to reach out if more details are needed!

jQuery

function displayCountries = (filteredResults) => {
    $countries.append(`
          <p>Choose your country</p>
          <form class="countriesFlex"></form>
    `);

    filteredResults.forEach((result) => {
          $('.countriesFlex').append(`
              <input type="radio" id="${result.origin}" name="country" value="${result.origin}" aria-hidden="true">
              <label for="${result.origin}" aria-label="click to display item with origin of ${result.origin}" tabindex = "0"> ${result.origin}</label>
          `);
   });
};

CSS

label,
input,
[type=submit],
a {
    border: 3px solid transparent;
    &:hover {
        transform: scale(1.1);
    }
    &:focus {
        border: 3px solid $accent;
        outline: none;
    }
}

.countries {
    padding: 0px 0px 75px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .countriesFlex {
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        padding-top: 30px;
        max-width: 600px;
        margin: 0 auto;
    }
    label {
        padding: 15px 30px;
        margin: 10px;
        border-radius: 5px;
        background-color: $secondary;
        color: $primary;
        text-align: center;
    }
    input {
        visibility: hidden;
        width: 0px;
    }
}


  [1]: https://jsfiddle.net/conno/rdantc03/8/

Answer №1

The issue lies in how the click event is managed.

When you click on a <label>, it triggers any associated checkbox or radio <input>.

However, since labels are not meant to be accessed via keyboard, there is no default behavior for pressing the Space key (as would activate a radio input) to trigger a click event.

Your current CSS selector [name=country] activates the click event.

This works with mouse clicks because the label passes on the event to the corresponding input element which has the click event handler attached to it.

But when you press Space while a label is selected, this does not get passed to the related <input> where your handler is located.

A possible solution could be:

$('label').click(function() {
       var labelID = $(this).attr('for');
       $('#'+labelID).trigger('click');
});

This code snippet should pass the click event to the input (Note: the effectiveness of this code is untested, alternatively, you can listen for the <space> key press on the label and direct it to the relevant input).

For proper labeling practices, I suggest referring to this article on creating custom keyboard-accessible radio buttons using <fieldset> and <legend> elements, along with styling tips for custom radio buttons that can be customized.

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

Listen for the modified value in a directive with AngularJS

I am currently working on implementing a directive that can draw a chart based on specified values. What I am aiming for is to pass the data necessary for the plot directly from the template rather than using ng-model, as my current solution requires. I ...

Is it possible to remove without delay using .ignore()?

Check out this demonstration: <div id='example'> Hello </div> $('#example').fadeOut(600).delay(600).remove(); I am trying to fade out an element and then remove it, but it seems like the .remove() function is not hono ...

The datapicker feature permits the selection of dates that are considered invalid

If a user does not know the exact date, how can I allow them to input something like '99/99/9999'? $('#date').datepicker({ language: "pt-BR", autoclose: true, format: 'dd/mm/yyyy', todayHighlight: true }) Whe ...

Dealing with the element not present error in Protractor can be managed by using various

Is there a way to achieve similar Exception handling in Protractor as we can with Selenium webdriver in Java? When dealing with element not found exceptions, what is the most effective approach to handle them using Protractor? ...

Having trouble accessing the name property of a select dropdown in Material UI React

Currently, I am facing an issue with implementing a select dropdown. When handling the onChange method, I am encountering a situation where event.target.name is undefined. Specifically, when I choose the 1st option, I want to be able to access 'Englis ...

The spread operator in Vuex is causing compilation errors with babel, as it continuously results in a module build failure

Currently, I am utilizing a spread operator within my mapGetters object. It is crucial to use a specific babel-preset-stage for proper ES6 compilation. Even after installing babel-preset-stage-2 via npm, I encountered the following error: ERROR in ./~/bab ...

Removing a dynamic TypeScript object key was successful

In TypeScript, there is a straightforward method to clone an object: const duplicate = {...original} You can also clone and update properties like this: const updatedDuplicate = {...original, property: 'value'} For instance, consider the foll ...

Incorporating an element into a nested array

I have an array stored in a variable called 'items' with various products and their attributes. I am looking to randomly generate a popularity score between 1 and 100 for the items that currently do not have one. This is my current array: const ...

"Implementing filtering logic for an array of objects with multiple conditions in a React application

I am looking to apply filters to a person list based on criteria such as city, job, age, and gender. How can I apply filters based on five conditions in React? I tried using filter chaining but it did not work for me. In the useEffect hook, I applied indiv ...

Can you explain the distinctions between Rundeck and Quartz in terms of their job scheduling capabilities?

In my quest for a job scheduler compatible with both node.js and Rundeck, I stumbled upon Quartz. However, despite my efforts to grasp the differences between Quartz and Rundeck, I find myself at a loss. Any assistance on this matter would be immensely a ...

Retrieving model data using jQuery

This particular section displays a model property alongside a submit button. @Html.LabelFor(m => m.Password) @Html.PasswordFor(m => m.Password, new { id = "txtLgnPasswordReset", @class = "form-control avoid-copy-paste" }) <div class="row vert-of ...

Combining the first name, last name, and code in Javascript

I'm attempting to combine the initial letter of both names with the randomly generated code. var firstname = prompt("Please input your first name."); var lastname = prompt ("Please input your last name."); if (amountCorrect >= 4){ ...

The onClick function within the .map function is continuously triggered

I am encountering an issue with my code where a Dialog confirmation prompt from Material UI keeps getting called unexpectedly. The problem seems to arise when I add a value to the function that is triggered by a button click within a loop of an array usi ...

Creating an animated link with a dynamic underline featuring a trio of vibrant colors

I'm having trouble figuring out how to animate my links on hover with 3 different colors. I attempted using the linear-gradient property but it doesn't seem to be working. Any suggestions on how to proceed? Below is an example of what I'm a ...

TRPC fails to respond to the passed configuration or variables (e.g., when enabled is set to false)

Recently started using trpc and I'm trying to grasp how to utilize useQuery (which I've previously worked with in react-query): const IndexPage = () => { const { isLoading, data, isIdle } = trpc.useQuery([ "subscriber.add", { email: ...

Using JavaScript and HTML, create a click event that triggers a drop-down text

Can anyone help me with creating a dropdown feature using JavaScript, HTML, and CSS? I want to be able to click on the name of a project and have information about that project show up. Any suggestions on how I can achieve this? Thanks in advance! ...

Updating to a newer version of jQuery causes issues with pop-out submenus

Looking for a way to create a menu with pop-out submenus? Here's an example using jQuery: <script type="text/javascript"> $(document).ready(function() { var hoverAttributes = { speed: 10, delay: 1 ...

What could be causing my React component to only render after pressing ctrl + shift + R?

I am facing an issue with my chrome extension where it only appears after refreshing the page using ctrl + shift + r. However, there is a new problem that arises when I click on a link that refreshes the page, causing the extension to disappear and requiri ...

Prevented: Techniques for providing extra cushioning for a button, but with the condition that it contains an external icon

How can I apply padding to a button only if it contains an external icon? If the button has an external icon, I want to give it padding-right: 30px (example). However, if there is no external icon present, then the button should not have the 30px padding. ...

CSS page header not appearing on all pages when printing from TryitEditor in Safari on iOS

Experimenting in W3's TryitEditor, I am currently using the following CSS: p.hr { position: running(header) } @media print { .pagebreak { page-break-before: always; } @page { @top-center { content: element(header); } } } Ther ...