Hovering over the TR element will result in an impact on B

I created a unique table layout that looks like this:

<table>
   <tr class="anHour">
      <td class="hour" rowspan="3">9:00</td>
   </tr>
   <tr class="place">                                       
      <td>Place A</td>
      <td></td>
      <td></td>
      <td></td>
   </tr>
   <tr class="place">
      <td>Place B</td>
      <td></td>
      <td></td>
      <td></td>                               
   </tr>
</table>

There are two key features I want to implement:

  1. When hovering over tr.place, it should also activate the hover effect on tr.anHour (or td.hour).
  2. When hovering over tr.anHour (or td.hour), it should activate the hover effect on all tr.place elements.

Any ideas on how to achieve this? Let me know!

Answer №1

Creating this effect purely with CSS is not possible. While you can change the style of the place elements using a general sibling combinator (

.anHour:hover ~ .place { /* styles */}
), you cannot determine the styling of an earlier sibling based on the state of a later one.

Instead, you can utilize the mouseenter and mouseleave events to add or remove classes. Although it may not be exactly the same as triggering hover, it achieves a similar outcome.

Below is an example that showcases CSS for one part and JavaScript for the other so you can observe both in action:

Array.prototype.forEach.call(
  document.querySelectorAll(".place"),
  function(place) {
    place.addEventListener("mouseenter", handlePlaceMouseEnter);
    place.addEventListener("mouseleave", handlePlaceMouseLeave);
  }
);
function handlePlaceMouseEnter() {
  this.parentNode.querySelector(".anHour").classList.add("hover");
}
function handlePlaceMouseLeave() {
  this.parentNode.querySelector(".anHour").classList.remove("hover");
}
.anHour:hover ~ .place, .place:hover {
  color: green;
}
.anHour:hover, .anHour.hover {
  color: blue;
}
<table>
   <tr class="anHour">
      <td class="hour" rowspan="3">9:00</td>
   </tr>
   <tr class="place">                                       
      <td>Place 1</td>
      <td></td>
      <td></td>
      <td></td>
   </tr>
   <tr class="place">
      <td>Place 2</td>
      <td></td>
      <td></td>
      <td></td>                               
   </tr>
</table>

Answer №2

Styling with CSS

.anHour:hover ~ .place {
    background: #9bcc41;
}  

The ~ symbol is a successor siblings selector that only works for succeeding elements. For previous siblings, you can use jQuery by adding "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" and using the hover() function.

jQuery Script

$(document).ready(function(){
    $('.place').hover(function(){ //Style on hover 
        $('.anHour').css({"background": "#9bcc41"});
    },    
    function(){ //Style when not hovered
        $('.anHour').css({"background": "none"});   
    });         

});

Answer №3

Below is an example of using JQuery to add hover effects:

$('.place td').mouseenter(function() {
$('.anHour').addClass('hover');
}).mouseleave(function() {
$('.anHour').removeClass('hover');
});
.anHour.hover {
    background-color: red;
}
.anHour:hover ~ tr {
    background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
   <tr class="anHour">
      <td class="hour" rowspan="3">9:00</td>
   </tr>
   <tr class="place">                                       
      <td>Place 1</td>
      <td></td>
      <td></td>
      <td></td>
   </tr>
   <tr class="place">
      <td>Place 2</td>
      <td></td>
      <td></td>
      <td></td>                               
   </tr>
</table>

Answer №4

If you want to apply CSS styles based on certain conditions, you should familiarize yourself with CSS selectors.

One method is to ensure that the element you want to modify appears before the element that triggers the change.

CSS

.place:hover ~ .anHour {
    /*Include your CSS code here*/
}

The above code snippet will activate the specified CSS properties on all sibling elements with a class of .anHour when any element with a class of .place is hovered over.

Alternatively, you can achieve this using jQuery.

jQuery

$(function() {
  $('.place').hover(function() {
    $('.anhour').css();
  }, function() {
    // Reset the CSS properties on mouseout
    $('.anhour').css();
  });
});

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

Learn the art of separating a CSS attribute and its value with Jquery

I'm currently facing a challenge with splitting a CSS value in jQuery. I have a styled div like this: .loadingTank { display: inline-block; border: 2px solid black; margin: 15px; max-width: 350px; height: 500px; width: 30%; ...

Using Node.js, we can create a program that makes repetitive calls to the same API in a

My task involves making recursive API calls using request promise. After receiving the results from the API, I need to write them into an excel file. Below is a sample response from the API: { "totalRecords": 9524, "size": 20, "currentPage": 1, "totalPage ...

The confusion arises from the ambiguity between the name of the module and the name of

In my current scenario, I am faced with the following issue : module SomeName { class SomeName { } var namespace = SomeName; } The problem is that when referencing SomeName, it is pointing to the class instead of the module. I have a requireme ...

Trying out a TypeScript class created with Jasmine testing

Looking to test a TypeScript class using Jasmine, I encountered an error when running jasmine spec/movieSpec.js. The error message "TypeError: Movie is not a constructor" appears. My files are as follows: src/movie.ts export class Movie { private name: ...

Can AngularJS filter be used with an array of strings for sorting?

Understanding how to implement an Angular filter to solve a problem I'm facing is proving to be quite challenging for me. Let's take a look at a simple example of my data structure, which consists of an array of tasks: var tasks = [ { Title ...

When async is set to true in an Ajax call, the response may come

I recently developed a function that works perfectly fine when the async option is set to false. However, I am now looking to achieve the same functionality and return value but with async set to true. function Call(param, proc) { var url = "../../ ...

Generate dynamic Bootstrap Carousel slides with unique hashtag URLs

I've been attempting to connect to various slides within a bootstrap carousel from a different page but have had no success. Here is an example of what I'm trying to achieve: <a href="services#slide2">Link to Slide 2</a> For refere ...

The issue of banding caused by Bloom and Antialiasing in Three.js rendering

I'm attempting to incorporate a glowing effect into my scene. To achieve this, I understand that using a bloom filter with the EffectComposer is the ideal approach. However, I've encountered an issue where utilizing the EffectComposer compromises ...

Display a notification when a user logs in for the very first time

Can someone help me with this specific scenario? My application needs to display an alert when a user logs in from a browser other than Chrome, informing them that the application works best on Chrome. The alert includes two options: "Don't show me a ...

Tips for adjusting the size of a background image using zoom

Currently, I am in the process of creating my first website. At this stage, it consists of a background image, an image, and a non-functional password input section. Everything seems to be going well so far. The background image fits perfectly on the scree ...

Time whizzes by too swiftly

After attempting to create an automated slideshow with clickable buttons, I encountered a problem. The slideshow functions properly, but as soon as I click one of the buttons, the slideshow speeds up significantly. This is what I implemented in my attempt ...

Why is it that one of my useQuery hooks is returning a value while the other one is returning undefined?

I'm currently facing an issue with React Query where one of my useQuery hooks is logging undefined while the other one is displaying the correct data. Both functions are async and perform similar tasks. I've been troubleshooting this problem for ...

The Bootstrap Navbar appears hidden beneath other elements on mobile devices

While using bootstrap to style my header contents, I encountered a strange issue. The navbar that appears after clicking on the hamburger menu is displaying behind all the components. Even though I've set the z-index to the maximum value, it still doe ...

Show whether the day is even or odd with the JavaScript getDay object

I'm currently working on a task where I need to show the text "Even Day" if the day of the month is 2, 4, 6..., and "Odd Day" for days such as 1, 3, 5, and so on. I attempted to achieve this by setting up an array linked to the getDay object, but unfo ...

What is the best way to use jQuery to toggle the hidden class on an inner div when a button inside a card is clicked?

How can I implement jQuery functionality to toggle the hidden class on an inner div by clicking a button inside a card? I attempted to create a container div containing multiple cards. The issue arises when I click the button - it only opens the panel, bu ...

Maintain the aspect ratio of an image within a flex container when the height is adjusted

I'm currently facing an issue with an image in a flex container. My goal is to maintain the image's ratio when the height of the container or window is decreased or resized. Check out this sample fiddle html, body { height: 100%; } .wrappe ...

The input value in the HTML form was altered momentarily before reverting back to its original state

Researching this topic was quite challenging, but keep reading to find out why. My objective is to detect any changes in a form field so that I can enable the "Save" button. While this seems easy enough, there's a catch. If the user reverts the input ...

Problems arise with identification of asynchronous functions

My async functions have been used successfully in various projects, but when incorporating them into a new project, they suddenly stopped working. One of the functions causing trouble is: const ddbGet = async (params) => { try { const data ...

Conceal specific image(s) on mobile devices

Currently, I am facing an issue on my website where I need to hide the last two out of six user photos when viewed on mobile or smaller screens. I believe using an @media code is the solution, but I'm unsure about the specific code needed to achieve t ...

Is Angular causing the issue with Datalist not functioning in IE9?

Autocomplete feature using datalist in Angular works perfectly on all browsers except IE9. I even tried the following workaround: <datalist id="associates"> <!--[if IE 9]><select disabled style="display:none"><![endif]--> <o ...