When the pointer events for table data are disabled, the table row (<tr>) will not be able to receive any pointer events

I have a basic table with different data displayed in rows and columns. I want to make the entire row clickable so that when clicked, something happens.

By default, the <td> elements inside the table act as event targets, but I specifically want only the table row to be the target for the click event. To achieve this, I tried the following:

td {
  pointer-events: none;
}

tr {
  cursor: pointer;
}

Although this approach worked fine when using <div> tags containing <span> elements, it doesn't behave the same way with <tr> and <td>. Even after adding pointer events, the table row still does not respond to any click events.

Is there anyone who knows of a workaround or solution to this problem?

Answer №1

According to the information provided on MDN

none

The element will not be the main target for mouse events; however, its descendant elements can still receive mouse events if they have different pointer-events values set. In such cases, event listeners attached to this parent element may respond accordingly as the events propagate through the descendant elements during capture/bubble phases.

By disabling pointer-events on td elements, clicking directly on the td itself (instead of the margin around it within the tr) won't register any action.

Instead, you can bind a click event to the tr, or even higher up in the hierarchy like the tbody or table. Here's an example:

document.querySelector("table").addEventListener("click", function(e) {
    // Check if click occurred on a tr within this table
    var tr = e.target.closest("tr");
    if (tr && this.contains(tr)) {
      // Toggle highlight if yes
      tr.classList.toggle("highlight");
    }
  });
.highlight {
    background-color: yellow;
  }
<table>
    <tbody>
      <tr>
        <td>One</td>
        <td>Two</td>
        <td>Three</td>
      </tr>
      <tr>
        <td>One</td>
        <td>Two</td>
        <td>Three</td>
      </tr>
      <tr>
        <td>One</td>
        <td>Two</td>
        <td>Three</td>
      </tr>
    </tbody>
  </table>

(The contains part ensures that clicks outside of a tr within this table are not captured.)

For additional reference:

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

Unable to display a cube using Three.js on a custom WebGL layer in Mapbox GL JS

Attempting to showcase the cube using this example: Add a 3D model. The example functions fine up to version 117 on three.js. However, starting from version 118, the cube disappears immediately after refreshing the page. After reviewing the changelog, it ...

Incorporating transparent padding to PNG images for a three-dimensional effect

I'm currently facing an issue where I need to resize images to the Power of 2 in order to load them into Three.js. The resizing process involves a specific algorithm: var w = powerOf2Down(this.width); var scale = w / this.widt ...

Numerous slideshows using identical scripts

I am facing an issue with my code for multiple slideshows. Currently, it works fine for a single slideshow but when I have multiple slideshows and mouse over any of them, all the slideshows start running due to sharing the same class. However, if I try to ...

JavaScript failing to load following PHP header() redirect

I've set up a page that allows users to sign in by filling out a basic form, which then sends the data to a separate PHP script for validation. After the validation process is complete, the PHP script uses the header() function to redirect the user to ...

HTML validation produces mistakes

I encountered an issue when using Google Fonts and received the following error related to a specific link: <link href="http://fonts.googleapis.com/css?family=Lato:100,300,400,700,900,100italic,300italic,400italic,700italic,900italic|Montserrat:700|Mer ...

Is there a way to position a pseudoelement behind its parent yet still in front of its grandparent using absolute positioning?

Imagine the following scenario: <div id="one"/> <div id="two"> <div id="three"/> </div> <div id="four"/> I am trying to apply a pseudoelement to three that appears behind it but in front of two (and any other ancestors if ...

Singularize button/solo in the center

How can I make this button align perfectly center on both PC and Mobile as a container? I have attempted the following CSS and HTML code: <section id="jour"> <div class="container"> <button class="btn1">Jeudi 12</button> ...

An issue has occurred while trying to execute the npm start command within PhpStorm

When I try to run npm start on Windows' command prompt, it works fine. However, I encounter errors when running it in the PhpStorm Terminal. You can see the errors here. Is this a result of them being different platforms or could it be related to som ...

Developing a Cloud Function for Stripe that is taking forever to finalize writing data to Firestore

Currently, I am facing an issue with my Google Cloud function (provided below) that handles webhooks from Stripe payments and stores some data in Firestore. The problem is that it seems to hang for approximately 3 minutes before completing. const Stripe = ...

Design interactive diagrams in React js with the help of Fluent UI

I am looking to build a flowchart with interactive buttons in React JS using Fluent UI components. Unfortunately, I have been unable to find a suitable solution within the Fluent UI library for creating Flow Charts. If anyone has expertise in this area a ...

Tips for Importing HTML Table Data Line by Line into SQL Table

I have the following code here. I am looking to insert HTML table data row by row into an SQL database. However, with this current code, only the last row is being stored in the database table. I believe I need to implement a loop to read the data from e ...

How can you insert a title or key into an array containing numerous objects using javascript?

I have a function that extracts data from files and returns it in an array const extractTestCases = () => { const filesArray = [] const filterTestSuite = files.filter(test => test.includes('.test.ts')) filterTestSuite.forEach(te ...

Problem with the visibility of the drop-down menu when hovering over it

I am currently developing a ReactJS application using reactstrap. One of the components I have created is a dropdown with submenus nested inside. My goal is to make the submenus appear when hovering over the dropdown, and if there are multiple dropdowns ( ...

Is the function failing to generate an input field?

Trying to implement a function that triggers an input field to appear upon clicking an element using the onclick event. Let's take a look at the code below. <!DOCTYPE html> <html> <body> <button onclick="createMessage()">New ...

Experiencing the dreaded "Syntax Error: Unexpected Token" message while trying to execute a function via ajax

I keep encountering Syntax Errors when using ajax to call a php file. Uncaught SyntaxError: Unexpected token F Uncaught SyntaxError: Unexpected token F Uncaught SyntaxError: Unexpected token F Uncaught SyntaxError: Unexpected token F Uncaught SyntaxError: ...

Access language options in the dropdown menu using keyboard shortcuts

I have a dropdown that appears when the mouse hovers over it: Is there a way to also trigger the dropdown to appear when the user tabs to it? I've tried following this thread for guidance: Selecting div in custom dropdown using Keyboard (Up, down and ...

Using Java's Selenium WebDriver to select and click on an item in a list

<div class="class0"> <div class="class1"> <input tabindex="4" id="pqr" autocomplete="off" type="text"> </div> <ul class="class2"> <li class="group-result">polka</li> <li class="active-result g ...

Adding elements to an array in jQuery by pushing them

I have been attempting to populate an array with objects, but for some reason, the array is not filling correctly. The last value seems to be set in all positions of the array. Below is the code snippet I am using: var matrixprice = 5; var qualifiedDate ...

Tips for patiently waiting for a series of asynchronous calls to successfully complete

I have a scenario where I am dealing with multiple asynchronous calls that are dependent on each other's success. For example: function1 = () => { /*Some required Logic*/ return fetch("myurl") .then((json) => { functi ...

What steps can I take to personalize Material UI within a React application?

As someone who is new to this UI framework and React, I have been tasked with developing an application that requires more design patterns. I specifically chose a framework that does not depend on jQuery code. However, I am facing challenges when it comes ...