Displaying a different background color on a colgroup element in CSS when a scroll is

My webpage contains one or more large and complex tables, where I use JQuery to add background-color to tr and colgroup elements when hovering over the table(s).

An issue arises when there are multiple tables on a page that extends beyond the viewport with a visible scrollbar. The problem is that the first table does not get affected by the hover effect. To experience this firsthand, run the code snippet below in fullscreen mode. Interestingly, this behavior does not occur in Internet Explorer.

$("table").delegate('td','mouseover mouseleave', function(e) {
  var $table = $(this).closest('table');
  if (e.type == 'mouseover') {
    $(this).parent().addClass("hover");
    $table.children("colgroup").children("col").eq($(this).index()).addClass("hover");
  } else {
    $(this).parent().removeClass("hover");
    $table.children("colgroup").children("col").eq($(this).index()).removeClass("hover");
  };
});
.hover {
  background-color: #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="margin: 60px;" cellpadding="10" cellspacing="0">
(decided to showcase only content within one of the tables)

What could be causing this strange behavior?

Answer №1

Utilizing the nth-child(n) selector, I created a workaround that eliminates the necessity of using col and colgroup.

$("table").on('mouseover mouseleave', 'td', function(e) {
    if (e.type == 'mouseover') {
        $(this).closest("table").find("tr td:nth-child(" + ($(this).index()+1) + ")").addClass("hover");
        $(this).parent().addClass("hover");
    } else {
        $(this).closest("table").find("tr td:nth-child(" + ($(this).index()+1) + ")").removeClass("hover");
        $(this).parent().removeClass("hover");
    };
});

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

Utilize the splice function when resizing the window based on specific breakpoints

On a series of div elements, I have implemented some JS/jQuery code that organizes them by wrapping every three elements in a container with the class .each-row. <div class="element"></div> <div class="element"></div> <div class ...

Next.js encountered an issue: React.Children.only function was expecting to receive only one React element child, but received more

I have created a component named Nav located in the components directory. Here is the code for it: import Link from 'next/link'; const Nav = () => { return( <div> <Link href="/"> <a> Home </a> ...

The hover selector in Material UI and React is not functioning as expected

My code is experiencing an issue where the hover selector does not appear to be working. Although all other styling aspects function correctly, the hover effect is not visible. import React from "react"; import Paper from '@material-ui/core/Paper&apo ...

Accessing form data from Ajax/Jquery in php using $_POST variables

Thank you in advance for any assistance on this matter. I'm currently attempting to utilize Ajax to call a script and simultaneously post form data. While everything seems to be working correctly, the $POST data appears to come back blank when trying ...

The image filter plugin is functioning properly in one project, however, it is not working in

After successfully using a plugin from w3schools to filter elements in one project, I encountered an issue when attempting to implement it in another project. Here is the link to the problematic code pen: https://codepen.io/zakero/pen/mZYBPz If anyone ca ...

The exceptional speed of jQuery's each method sets it apart

I'm currently facing an issue where I am unable to successfully add the attribute data-size to my parent div. My code snippet is as follows: var width, height; $(".galerie img").each(function() { width = this.naturalWidth; height = this.naturalH ...

Which is more efficient: Editing the image 'src' or replacing the 'img' tag within a Div?

var currentImage = 0; var totalImages = 8; $(document).ready(function(){ $("#next-img").click(function(){ currentImage = currentImage + 1; if (currentImage > totalImages) {currentImage = 1;}; $(".img-class").attr('src',"http://examp ...

AngularJS - Oops! Looks like we encountered an error: [$injector:modulerr]

I have a client-server application and I am facing an issue with this error message: "Uncaught Error: [$injector:modulerr]". Despite searching for solutions, none of them seem to be fixing the problem. Here is my client-side code: angular.module('m ...

Exploring the relationship between Drupal and Ajax

I'm facing an issue with a custom module that creates a table with buttons, triggers an Ajax call to load a form in a div when clicked. The hook menu responsible for this function is /myapp/get_form. The problem arises when I try to save data from th ...

Is it possible to use JavaScript for detecting third-party videos?

I'm currently developing an HTML5 video player that also has a fallback to flash. One of the challenges I am facing is that the video content is being provided by various third-party sources. It seems that some of these third parties serve videos bas ...

What is the best way to display form input fields depending on the selected value?

I've been struggling with a seemingly simple issue that I can't seem to find the right solution for, even after scouring Google. I have a form field for input and a select field with various values, including an "Other" option. Here's what ...

Ways to change the chart type in ApexCharts

I'm seeking a way to change the chart type of an existing ApexCharts that has already been rendered. After reviewing the methods, I attempted to use the updateOptions() method, but encountered the error: Uncaught TypeError: Cannot read property &apos ...

How do you properly bind multiple events in jQuery and then unbind just a few of them?

Is it possible to bind multiple events, then unbind a couple of them? This is what I'm trying to achieve: When hovering over the element, the background color changes and changes back when hovering out. But when clicking the element, I want to disabl ...

JavaScript enables the deletion of a class

In HTML 2, I am able to show different statements based on the scenario. These statements are styled using the Bootstrap alert class. The goal is to ensure that when new data is sent, any old communication disappears without causing overload on the page. ...

Link JSON in JavaScript using field identifiers

I've been working on incorporating JSON data into my JavaScript code, and the JSON structure I am dealing with is as follows: { "status":"ok", "count":5, "pages":1, "category":{ "id":85, "slug":"front-page-active", "title":"Front ...

Trouble with CSS and JS tabs not activating when clicked?

I am experiencing issues with a navigation (organized in tabs) that is not functioning on this page, but it works correctly on this page using the same method for inclusion. When clicking "Norway" on the top left, the navigation opens but switching to the ...

Transform the prototype method into jQuery functionality

After finding this function online and testing it, I realized that it works fine on its own. However, the rest of my document consists of jQuery functions, so I would like to convert this one to jQuery as well. Additionally, I have encountered errors when ...

What is the best way to ensure the width of the div is adjusted to fit the table it contains?

What is the best way to adjust the width of a div containing a potentially wider-than-screen table? Despite setting padding: 10px for the div, the right padding disappears when the table exceeds the screen width. Below is an example code snippet: <div ...

Optimizing web layouts to fit on a single page is the most effective

Struggling to create a print layout for my web page that seamlessly transitions from the digital realm to the physical world. Utilizing HTML and CSS has presented challenges in aligning the "digital" design with a "real printable" page due to uncertainties ...

Attempting to place the search bar within the navigation bar

Having trouble positioning a search bar in my nav bar, I'd like it to show on the right side without overlapping any other elements. Tried relative and absolute positioning with no success so far. Any assistance would be greatly appreciated, thank yo ...