What is the best way to choose a table row <tr> along with the one directly before it?

In the table I have, the first column spans over two rows while the following columns are split into two separate rows.

For this table, I need multiple instances of these "doubled rows", which is not a problem so far.

To style it with a zebra pattern, I use the following CSS code in react/glamorous format:

const StripedTable = glamorous(Table)({
  '& th, & td': {
    padding: '4px',
  },
  '& tfoot tr': {
    background: '#DDDDDD',
  },

  '@media screen': {
    ['& > tbody:nth-of-type(even), ' +
    '& > tbody:only-of-type > tr:nth-of-type(even)']: {
      backgroundColor: '#EEEEEE',
    },
    ['& > tbody:not(:only-of-type):hover, ' +
    '& > tbody:only-of-type > tr:hover']: {
      background: '#DDDDDD',
    },
  },
},
  ({doubleRow}) => {
    if (doubleRow) {
      return {
        '@media screen': {
          // overwrite standard striped coloring that is used for "normal" table rows
          ['& > tbody:nth-of-type(even), ' +
          '& > tbody:only-of-type > tr:nth-of-type(even)']: {
            background: '#FFFFFF',
          },
          // Think in groups of 4 and color every two rows identical
          ['& > tbody:nth-of-type(even), ' +
          '& > tbody:only-of-type > tr:nth-of-type(4n), ' +
          '& > tbody:only-of-type > tr:nth-of-type(4n-1)']: {
            background: '#EEEEEE',
          },
          ['& > tbody:not(:only-of-type):hover, ' +
          '& > tbody:only-of-type > tr:hover,' +
          '& > tbody:only-of-type > tr:hover + tr']: {
            background: '#DDDDDD',
          },
          // FIXME hovering groups don't fit when mouse over even rows
          ['& > tbody:only-of-type > tr:nth-of-type(even):hover,' +
          // the following does not work of course, but it should show
          // what is intended. "- tr" can't work, as html works from top to
          // bottom only
          '& > tbody:only-of-type > tr:nth-of-type(even):hover - tr']: {
            background: '#DDDDDD',
          },
        },
      };
    }
  }
);

The problem I am facing can be better explained through the comments. How do I select, for example, the second row that I hover over together with the previous one? Inserting a tbody around every two rows and selecting it is not an option.

EDIT: Here is the HTML structure of one of the rows in JSX. Note that the DOMs are styled differently and have their own component, but their purpose is the same as tr and td:

  <TableRow>
    <TableData rowSpan="2">
      {'Title of Row'}
    </TableData>
    <TableData>
      {data1}
    </TableData>
    <TableData>
      {data2}
    </TableData>
    <TableData>
      {data3}
    </TableData>
    <TableData>
      {data4}
    </TableData>
  </TableRow>
  <TableRow>
    <TableData colSpan="4">
      {veryLongText}
    </TableData>
  </TableRow>

Answer №1

Here is a potential solution that may meet your needs.

Vanilla JavaScript

let selectedRow = document.querySelector('.table').rows[1];
let previousRow = selectedRow.previousElementSibling;

Using jQuery

let selectedRow = $('.myTableClass tr').eq(1);
let previousRow = selectedRow.prev();

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 functionality of Protovis JavaScript is not supported within a dropdownlist's onchange event

I encountered an issue where one block of code works fine on its own, but when combined with another block, only one of them functions properly. The problem arises when I try to invoke a method from a dropdownlist using the onchange event, especially afte ...

Having trouble replacing using String.replace() in Javascript?

I am working on a project that involves parsing a website and retrieving information from a database. The code I have looks like this: var find = body.match(/\"text\":\"(.*?)\",\"date\"); After running the code, I get the fo ...

Enhancing VueDraggable (sortable.js) with drag out feature

I am currently utilizing VueDraggable for effortless and efficient sorting of elements. However, I am in need of a feature that detects when a dragged element leaves its parent so that a particular function can be executed. To provide some context, think o ...

Expanding the length of an SVG stroke animation

I've been scouring the depths of the internet in search of a solution for a specific SVG path animation, but my attempts have been fruitless so far. I came across a codepen by Chris Coyier on CSS Tricks that I modified to achieve the desired effect, a ...

Having trouble converting the value into Vue2Editor

I'm currently utilizing vue2editor and encountering an issue on the edit page where I am attempting to pass the current content value to the vue2editor. Despite my efforts, the vue2editor still displays blank content as if no value was passed. Below ...

Spinning html/css content using JavaScript

Greetings! I am currently working on a demo site using just HTML, CSS, and JavaScript. Typically, I would use Ruby and render partials to handle this issue, but I wanted to challenge myself with JavaScript practice. So far, I have created a code block that ...

Whenever I create the code using javascript, padding seems to always show up

I have a basic stacked row layout that displays an upper box and lower box without any padding. <span id="route" class="small"> <div class="row" style="border-style:solid;"> <div class="col-md-12 col-12"> <p>UpperBox</p& ...

Issue with mouse movement in Selenium and Protractor not functioning as expected

Greetings! I am currently facing an issue in my Angular application with Openlayers3 map integration. There is a layer representing a building on the map, and when I try to click on a building during testing, it should trigger a side panel displaying image ...

specifying the input value pattern restriction in HTML5

Struggling to set a minimum and maximum value for an input text box. Despite my efforts, I haven't been able to make it work. How can I resolve this issue? The current pattern restricts values up to 10 but I need to allow values from 100 to 100000. Wh ...

Creating a Command Line Interface (CLI) application in JavaScript for the browser: A guide to simulating blocking I/O

Developing a CLI application becomes quite simple with a blocking I/O API like PrintLn / ReadLn, making the process smooth and efficient. However, the challenge arises when trying to create a terminal application that runs on a browser using JS. In this s ...

What is the best way to insert a tagline above a form?

I'm struggling to figure out where to place a tagline above my form. I want it to be neat and clean, but haven't been successful in finding the right spot. I attempted to insert an <h2> inside the form, but it doesn't look good at al ...

The $(window).load(function() function is unable to run once the entire document has finished loading

I have not been able to find the solution in the following circumstances: In an HTML document, I successfully load multiple other HTML files. However, within one of these included HTML files, specifically "navmenu.html," I want to execute a script with a ...

View the video in a separate window within the primary window

The question may seem unclear, but this image provides a better explanation: https://ibb.co/cVSQEv Is it possible for a pop-up window to appear when clicking on the image? Thank you ...

The profound responsiveness of properties in Vue.js components

I am working on creating a basic page builder that includes row elements, their child column elements, and finally the component that needs to be called. To accomplish this, I have designed an architecture where the dataset is defined in the root component ...

Creating a dropdown menu that seamlessly populates elements without any empty spaces

My team at the company is using a menu created by another developer. I recently added a dropdown selection to the first item on the menu, but I've run into an issue where the dropdown box does not fully fill the item, and I can't seem to fix it. ...

My PHP script encountering issues retrieving POST values

I am encountering an issue with my HTML login form and PHP processing. The form is supposed to POST values to /user.php, where my authentication class handles the login process. However, I am having trouble getting the form to actually submit the data. He ...

Creating a Form with Table-like Alignment Using Div Tags

Take a look at my code: <div> <label>First Name</label><input type="text" id='first_name'/><br /> <label>Last Name</label><input type="text" id='last_name'/><br /> <l ...

Displaying local PDF files with Vue.js - A comprehensive guide

I've been experimenting with different PDF viewers like pdf.js and vue-pdf, but I'm encountering some issues. With vue-pdf, I am able to make it work, but I can't seem to render local files. And when trying out pdf.js, I attempted to downlo ...

Redirecting pages without a hash portion

In my Express.js app.js file, there is a get route that looks like this: app.get('/admin', function(req, res, next){ if(req.isAuthenticated()) { return next(); } res.redirect('/admin/login'); },Routes.Admin.Index); When a ...

The prototype object of the Datatable request parameter is missing

When attempting to make a datatable ajax request, I encountered an unexpected result on the server side. The console is logging the data as shown below: [Object: null prototype] { draw: '1', 'columns[0][data]': 'no', &ap ...