Steer clear of using mouse-over effects and focus highlighting in CSS and jQuery

I am looking to implement a feature where only keyboard navigation triggers row highlighting in a table, rather than mouseover events.

When using the tab key to move through the rows, I want the highlight effect to be activated.

<table>
   <tr><td>Row1</td></tr>
   <tr><td>Row1</td></tr>
   <tr><td>Row1</td></tr>
</table>

To achieve this, I have defined the following CSS rule for highlighting:

table tr:hover{
  background: yellow
}  

You can view the code and functionality on JS Fiddle by clicking here.

The goal is to disable row highlighting on mouseover interactions but enable it when using the TAB key for navigation.

Answer №1

Check out this simple HTML/CSS demo. By using the pointer-events: none property and styling the row on :focus instead of :hover, you can achieve the desired effect.

Take a look at this fiddle showcasing two different examples:

  • The first example allows clicking or tabbing through the options.
  • The second example only permits tabbing through them.

Click here for the Fiddle

Answer №2

To achieve the desired result, you can utilize the :focus pseudo-class along with setting tabindex attributes on your rows. For a demonstration, check out this Fiddle: https://jsfiddle.net/3zsc7nok/

UPDATE:

If you want to prevent highlighting when clicking on rows using the mouse, you can employ a simple jQuery script like the one below:

$('tr').on("mousedown", function(event) {
  event.preventDefault();
});

Check out the revised Fiddle here for an updated example: https://jsfiddle.net/s00xgt2s/

Answer №3

Have you considered using jQuery instead of plain CSS for this task? You could try something like the following code snippet:

$("tr").keydown(function (e) {    
  if (e.which == 9) {     
    $(this).css("background", "yellow");
    e.preventDefault();
  }
});

I'm curious to hear if this solution works for you.

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

The sidebar should remain fixed on top when viewed on mobile devices, but not when viewed on desktop

Prior to beginning a new project, I'm interested in exploring the possibility of implementing a sliding sidebar for mobile and tablets. On desktop, however, I envision a fixed sidebar that is part of the main container layout as shown below: <div ...

Tips for creating a consistent vertical divider height between columns in bootstrap

I've encountered similar queries before, but the responses provided were not quite what I needed. While one answer came close to my desired outcome, it led to other complications that I am now facing. My main challenge is maintaining consistent vertic ...

jQuery address plugin does not function properly when content is being replaced

I have been utilizing the jQuery address plugin to allow for back-button functionality, and it has been working well. However, I am running into an issue when the link is within the AJAX content area. For instance: <div id="content"> <a href="e ...

Troubleshooting lpush errors in Node.js with Redis

I'm currently developing a web application using node.js and redis. The goal is to store each incoming request in a redis queue before saving it into a database. However, I keep encountering an error whenever the program executes the lpush command. Be ...

Placing two items at opposite extremes of a full-width display with a header

Despite the abundance of resources on CSS positioning, I constantly find myself revisiting this topic because it continues to perplex me with no clear solution in sight. Let's simplify things. Imagine a header div with a width of 100%, like this: &l ...

Do discrepancies exist between ' and " symbols?

Similar Question: When to Use Double or Single Quotes in JavaScript Difference between single quotes and double quotes in Javascript I scoured this site and did some internet sleuthing (in that order...) trying to find the answer to this burning q ...

Using AngularJS to bind a dynamically created form built in JavaScript

I am looking to dynamically build a form using JavaScript and utilize it within an AngularJS form controller. In the example provided below, the form is not rendering as HTML, and I aim to bind it to the model variable. http://jsfiddle.net/g6m09eb7/ ...

Verifying input fields prior to database entry

In order to insert records into the MySQL database, I need to validate the form fields before insertion to ensure they are filled out by users. The validation should occur on the onClick event of the submit button. I am using an annotation method related t ...

CSS can create a semi-circle shape using borders and outlines

Attempting to craft a CSS circle that mirrors the image below: ...with just a single div: <div class="myCircle"></div> solely utilizing CSS rules. No SVG, WebGL, DirectX, etc. permitted. My initial approach involved constructing a complete ...

Using Selenium to choose an element based on the text of its sibling element

I've been working on a selenium script for: My goal is to select the "Add to Compare" element for "Sony Xperia," but it's not being located. I've tried using both cssSelector and xpath, but I can't seem to figure out what I'm doin ...

Any suggestions on how to customize the tawk.to positioning within a Next Js script?

I've been attempting to adjust the positioning of the Tawk.to Sticky button on my Next js website using CSS, but it doesn't seem to be working. Below is the script within the Next Js script tag: <Script dangerouslySetInnerHTML={{ __html: ` ...

Creating a Node.js application using Express requires careful consideration of file structure and communication methods

Struggling to pass login form credentials to existing JavaScript files for logic and info retrieval. Login page contains a form with POST method. main.js handles the login: main.js module.exports = { returnSessionToken: function(success, error) { ...

Ways to solve the padding issue in Bootstrap 5

I am encountering an issue with setting the padding in my fluid container. The top-down padding is set to 3%, but the left-right padding of 15% is not working as expected. After debugging using Chrome Developer Tools, I noticed that the left-right padding ...

Unable to retrieve data from input field using ajax

After submitting the form, I am trying to retrieve the value from the input field named "phone" using jQuery. However, all I get is a null value and I can't figure out why it's not working. Strangely, there are no error messages in the console ei ...

Is it possible for a form submission through javascript to disrupt existing axios requests?

I am facing a peculiar issue reported by my users, but I have never encountered it myself because I cannot replicate it. In my vue.js component, there are two HTML forms. The first form is filled out by the user and upon submission, a function is triggere ...

Tips on properly styling HTML using the BEM naming convention

I'm trying to implement BEM naming in my project, but I'm having trouble with how to name elements within elements. BEM guidelines state that elements of elements should not exist. How should I approach naming them correctly? <div class="cont ...

Leverage ng-value attribute when working with radio buttons in AngularJS

I have recently started working with AngularJS and I am trying to create a selection using radio buttons that will save as a boolean value. However, when I use ng-value, the value saved in the database is null. Here is the HTML code: <label><in ...

What mechanisms does Vue employ to halt the browser from sending a server request when the URL in the address bar is modified?

When a link <a href='www.xxx.com'> is clicked in a traditional HTML page, the page is redirected to the new page. In a Vue application, we utilize the Router. See the code snippet below. Vue.use(Router); export default new Router({ mode ...

How can we effectively map Typescript Enums using their keys without relying on a Map?

Consider the following Enum instances: export enum TopicCategories { GUIDES = 'Guides', TASKS = 'Tasks', CONCEPTS = 'Concepts', FORMULAS = 'Formulas', BLOGS = 'Blogs' } export enum Top ...

Is the selection of values from a dropdown list dependent on the type of input method used?

I need help with writing JavaScript code to change the input type based on the user's selection from a drop-down list. For example, if the user selects "Title" or "Claim" from the drop-down list, a simple text input format should appear in front of th ...