Is there a way to illuminate a complete row by simply hovering over a span within one of the columns?

If I want to change the background-color of a div with classes "row" and "highlightThisRow" when hovering over a span with the class "fromThisSpan", how can I achieve that?

In a list, there are multiple rows with several columns. The span in question is located in the last column and has a hover effect defined in the CSS.

Although I managed to get the desired outcome for the first row by using IDs instead of classes for the div and span, I am struggling to apply the same effect to all rows.

Below are some unsuccessful attempts at achieving this using jQuery:

This code snippet represents the structure I am working with and does not include functioning code, only an outline:

<ul>
    @foreach(ItemType item in ItemCollection)
    <li>
        <div class="row highlightThisRow">
            <div class="col-md-4">
                ... item.contents1
            </div>
            <div class="col-md-4">
                ... item.contents2
            </div>
            <div class="col-md-4">
                ... item.contents3
                <span class="fromThisSpan"><a href="*" title="Delete this row">Delete</a></span>
            </div>
        </div>
    </li>
</ul>

Here is one failed attempt using the .on() method to attach the hover event:

$(".fromThisSpan").on("hover", $(".fromThisSpan").hover(
        (new function () {
            $(".highlightThisRow").css("background-color", "#fce2e2");
        }),
        (new function () {
            $(".highlightThisRow").css("background-color", "#ffffff");
        })
    ));

I also tried another approach based on a suggestion from Stack Overflow, but it didn't work either:

Source: Adding hover CSS attributes via jQuery/Javascript

$(".fromThisSpan").mouseenter(function () {
    $(".highlightThisRow").css("background-color", "#fce2e2");
}).mouseleave(function () {
    $(".highlightThisRow").css("background-color", "#ffffff");
});

[ UPDATE ] Solved After experimenting further, I found a solution that worked in my scenario:

$(".fromThisSpan").hover(function () {
    $(this).closest(".highlightThisRow").css("background-color", "#fce2e2");
}, function () {
    $(this).closest(".highlightThisRow").css("background-color", "#ffffff");
});

Answer №1

Utilize the closest method to obtain the corresponding highlightThisRow element of the selected element.

$(function(){
    $(".fromThisSpan").mouseenter(function () {
        $(this).closest(".highlightThisRow").css("background-color", "#fce2e2");
    }).mouseleave(function () {
        $(this).closest(".highlightThisRow").css("background-color", "#ffffff");
    });
});

Alternatively:

$(function(){
    $("body").on('hover','.fromThisSpan',function () {
            $(this).closest(".highlightThisRow").css("background-color", "#fce2e2");
        },function () {
            $(this).closest(".highlightThisRow").css("background-color", "#ffffff");
        });
});

Check out the demo:https://jsfiddle.net/7bgqfdkj/

Answer №2

attempt (this).parent

$("#fromThisSpan").on("hover", $("#fromThisSpan").hover(
        (new function () {
            $(this).parent("#highlightThisRow").css("background-color", "#fce2e2");
        }),
        (new function () {
            $(this).parent("#highlightThisRow").css("background-color", "#ffffff");
        })
    ));

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

Is it necessary to always include all styles for jQuery UI separately in my project?

While reading various articles, I have noticed a common suggestion to explicitly bundle each jQueryUI .css file, such as the answer provided on How to add jQueryUI library in MVC 5 project?. However, upon examining the .css files within the Content/themes/ ...

Incorporating jQuery to convert text into clickable links

Here is the situation: <div class="myclass"> Lorem ipsum dolor sit amet, http://example.com consectetuer adipiscing elit. </div> I am looking for a jQuery function that can be executed after the dom ready event. This function should locat ...

Combining D3.js tooltip positioning with CSS overflow styling

I am encountering an issue with displaying tooltips in D3 when combined with CSS overflow. Is there a solution available for this particular problem? CSS code: #chart1 { overflow-x: auto; overflow-y: hidden; max-width: 800px; max-height: 22 ...

Syncing Facebook Likes with Safari on iPad

Encountering an issue with the Facebook like button alignment specifically in Safari on iPad. It displays correctly in Safari on OSX. Check out this example. Any ideas on what CSS is needed to ensure it's left aligned in one row on the iPad? ...

Determine the type of a nested class within TypeScript

Utilizing nested classes in TypeScript is achieved through the following code snippet: class Parent { private secret = 'this is secret' static Child = class { public readSecret(parent: Parent) { return parent.secret } } } ...

The versatility of reusable Backbone components

As I search for the best way to ensure the reusability of Backbone views, I have come across various solutions but am unsure which one would best meet my needs. My goal is to create multiple widgets populated with real-time data and I require a base compon ...

Tips for anchoring an element when scrolling reaches its highest point

Is there a way to keep a div element fixed in its original place when scrolling through it, so that it locks into position once we reach a certain point? I've tried using CSS position:fixed; without success. I have noticed this technique on many webs ...

Wordpress tabs with dynamic content

I came across a code on webdeveloper.com by Mitya that had loading content tabs and needed the page to refresh after clicking the tab button. It worked perfectly fine outside of WordPress, but when I tried implementing it into my custom theme file, it didn ...

Javascript Library Issue: "Implicitly Declared Type 'Any' Error"

I am currently in the process of developing a JavaScript library that will interact with an API. My goal is to create a module that can be easily published on npm and utilized across various frameworks such as Angular or React. Below is the code snippet fo ...

Determine whether a JavaScript string exclusively consists of punctuation marks

In an if statement, I want to verify whether the given string contains any punctuation. If it does contain punctuation marks, I intend to display a pop-up alert message. if((/*regex presumably*/.test(rspace))==true||(/*regex presumably*/.test(sspace))==tr ...

Sending dynamic variables from JQuery to PHP using .post

I am encountering an issue where a JQuery variable is not being passed into PHP code within the same page. The JQuery variable seems to be triggering correctly (as I receive the confirmation alert), but PHP does not recognize it. How can I resolve this? ...

Steps for populating an ng-table with data retrieved from a REST web service that returns a JSON format

I am currently facing an issue while trying to load my table from a JSON response that I receive from REST web services in SpringMVC. The error message I received indicates that my REST method does not support the GET request. The URL mapped in my control ...

Enzyme fails to locate text within a div even though it is present

In my current test scenario, I have a set of data displayed in Material-UI Cards. The data is provided through a JSON array consisting of objects with fields like projectName, crc, dateCreated, createdBy, and lastModified. { projectName: 'P ...

Display data fetched from concurrent Ajax calls in a React Component

I am currently in the process of converting my original project to Reactjs. Since I am new to this and it's my first application, I could use some guidance on how to effectively implement this in a React-friendly way and enhance both my skills and the ...

Looking for a custom JavaScript color wheel with advanced features?

In search of a javascript color picker/wheel that allows users to easily select colors for our paint shop. Once the color is selected, it should automatically add the color value to the shopping cart. Our online store is operated using PrestaShop, so we ...

What are the steps to make ng-show functional in an AngularJS application?

I am attempting to implement a hover effect where an image is displayed over another image upon hovering over its container. I have been trying to achieve this using Angular and ng-show, but for some reason, the image with the ng-show attribute remains hid ...

Phaser 3 game app on iOS generated with Capacitor lacks audio functionality

I have developed a basic test app using Phaser 3 (written in Typescript and transpiled with rollup) and am utilizing Capacitor to convert it into an iOS application on my Mac. This excerpt highlights the key functionality of the app: function preload () { ...

Past methods: Obsolescence alert and caution

Trying to grasp the concepts of PHP & MYSQL, I have written this code which seems to be functioning properly. However, there are several "warnings" that I am unsure about. Nonetheless, PHP successfully connects to the database. Below are the relevant codes ...

Enhanced JavaScript Regex for date and time matching with specific keywords, focusing on identifying days with missing first digit

I have a specific regular expression that I am using: https://regex101.com/r/fBq3Es/1 (audiência|sessão virtual)(?:.(?!audiência|sessão virtual|até))*([1-2][0-9]|3[0-1]|0?[1-9])\s*de\s*([^\s]+)\s*de\s*((19|20)?\d\d) ...

enhancing the functionality of appended children by including a toggle class list

I'm having trouble toggling the classList on dynamically appended children. The toggle works fine on existing elements, but not on those added through user input. Any tips on how I can achieve this? I'm currently stumped and would appreciate any ...