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

Unable to execute script tag on PHP page loading

When I make an AJAX request to fetch JavaScript wrapped in <script> tags that needs to be inserted on the current page, how can I ensure that the code executes upon insertion? Here's the snippet I'm using to add the code: function display ...

Encountering an issue with ReactJS + Redux where the error message states: "Error in prop type: The Right-hand side of 'instanceof' cannot be called"

Currently working on a web app project in React with Redux for global state management. A puzzling issue has arisen - we're receiving warnings in the browser console. How can we resolve this? It seems related to prop types declaration, but the soluti ...

What could be causing my JavaScript alert to not appear on the screen?

Essentially, I've been attempting to trigger a Javascript alert using PHP. However, the alert isn't functioning at all. This is my echo statement that dynamically generates the alert echo "<script>alert('Uploaded file was not in the ...

Retrieve the visible text content of an element by utilizing various ids

I am currently working on a project using AngularJS with multiple conditions all sharing the same id. My goal is to extract text only from the condition that evaluates to true. Recently, I discovered a major bug in an app that I am preparing for release. ...

Tips for creating a dynamically responsive eye icon placement

When the screen width is less than 991px, the content is centered but the eye icon that is absolutely positioned goes off-center. This issue only occurs on devices with a small or medium screen size. The reason for using absolute positioning on the icon i ...

Explain the operation of recursive function calls in JavaScript

I’ve been working on converting the algorithm found in this Python code snippet into JavaScript. function divide(arr, depth, m) { if (complements.length <= depth) { complements.push(2 ** (depth + 2) + 1); } var complement = comple ...

Is it possible to incorporate HTML content into the metadata within the head section of a Nuxt application?

Received HTML content from the backend that needs to be incorporated into the meta tag of the HTML head using nuxt. Encountered an error when attempting to display the received content View Error Outlined below is the code implementation: Snippet of Code ...

The Iframe on the webpage is automatically refreshing once a submission is made, which is not a feature present in the original version of

Within my newsletter.html file, I have set up a newsletter where users can submit their email addresses, which are then stored in my JSON file. In homepage.html, I embedded an iframe from the newsletter.html file. Everything seems to be working fine, exce ...

After submitting a post, the click event of a button in IE 10 using jQuery is not

The uploadButton feature allows users to upload an image and store it locally. I utilize fileinput to select the image, and when uploadbutton is clicked, the image data is sent back to the server. While this functionality works smoothly in Chrome, I encou ...

How to make an AJAX request in jQuery / JavaScript after a specific time interval has passed

Here is the code snippet I'm working with: $('input.myinput').each(function(){ var id = $(this).val(); var myajax = function(){ $.ajax({ url: ajax_url, type: "GET", data: ({ code: id ...

After the loop completes, only the last row is inserted

Here is the issue I am facing with my info.php file. I am extracting the steam_id from the scammers table in this file. The problem arises when I display the results on the front page because all player parameters turn out to be the same. This happens beca ...

Tips for sending and retrieving parameters using the POST technique

Currently, I am in the process of building a user authentication form for my website using Javascript. I am utilizing Vue JS on the client-side and NodeJS with ExpressJS on the server-side. For the server-side functionality, I have implemented the followi ...

The script from 'URL' was declined for execution due to its MIME type of 'text/html' which is non-executable, in addition to strict MIME type checking being enabled

I encountered an error stating "Refused to execute script from 'URL' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled." The code that triggered this error is shown below. <!DOCTYPE htm ...

What is the best way to fix the lint check error within a Vue file's styling?

Is there a way to eliminate the red wavy lines on the screen? ...

Having trouble integrating a custom plugin with CKEditor?

I am currently using version 4.7 of CKEditor, which is the latest version available. I am facing an issue where I cannot see a new custom plugin that I have added to the toolbar. I have checked my basic example of abbreviation (abbr) plugin but couldn&apos ...

Passing a list variable to JavaScript from Django: A step-by-step guide

Currently, I am facing an issue while attempting to generate a chart using Chartjs and Django. The problem arises when transferring data from views.py to the JavaScript code. Here is a snippet of my code in views.py: def home(request): labels = [&quo ...

Is there a way to eliminate the space between the content inside a table cell and its borders?

I am struggling with a table setup that includes three columns and multiple rows. The first column contains an image, causing the cells in that row to resize based on the image size. When text is added to the 2nd and 3rd columns, it automatically centers w ...

Delaying loops with JQuery Ajax

I am attempting to implement a delay in my AJAX data processing so that the loop runs a bit slower. Here's the code I'm working with: $(document).ready(function (){ $('#button').click(function(){ $('#hide').show ...

Step-by-step guide on creating a sequential glowing effect for list items with CSS and JQuery

I would like to create a glowing effect on each icon individually. The idea is for each icon to glow for a brief moment and then return to its normal state before moving on to the next icon in line. I am considering using text-shadow for the glow effect an ...

Using JavaScript to show a prompt message inside an h1 tag within a newly created div

I have developed a basic JavaScript program that opens a prompt dialog when the div tag is clicked, allowing the user to enter text. The program then creates a new div element and displays the entered text above it. However, I am facing an issue where I wa ...