Ways to access the preceding DIV element closest to the current one

Hovering over the text My will cause the image myicon.png to fade out and back in:

<div class="mmItemStyleChild">
    <img src="theImages/myicon.png" class="mIcon vertAlign mmm1" id="mMW1" /> <img src="theImages/emptyImg.png" class="mSpacerStyle" /><span id="mMW" class="vertAlign mmm">My</span>
</div>

$('.mmm').hover(function () {
    $(this).closest('div').find('img').first().stop().animate({ 'opacity': .35 });
}, function () {
    $(this).closest('div').find('img').first().stop().animate({ 'opacity': 1 });
});

I attempted to do the same for the section below, but it is not working as intended:

<div class="mBtnMidS mBtnMidStyle">
    <div>
        <img id="mbS1" src="theImages/services_icon.png" class="mBtnIcon" />
    </div>
    <div>
        <span id="mbS" class="mbBtnText">Services</span>
    </div>
</div>

$('.mbBtnText').hover(function () {
    $(this).closest('div').find('img').first().stop().animate({ 'opacity': .55 });
}, function () {
    $(this).closest('div').find('img').first().stop().animate({ 'opacity': 1 });
});

To modify the above code to achieve a similar fade animation from the parent DIV (using either class 'mBtnMidS' or 'mBtnMidStyle') for the image, how can I go about it?

Answer №1

For the second code block modification, make sure to change it as follows:

$(this).closest('div')

Adjust it to read like this:

$(this).closest('div.mBtnMidS.mBtnMidStyle')

Previously, the closest div referred to the immediate parent of the span element, but you actually needed to target the parent of that div (which is the grandparent of the span).

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

Guide: Exchanging choices using jQuery and Address plugin

I am trying to find a way to exchange the values of two options with each other. I created a simple fiddle that successfully swaps input values, but when I tried it with select options, it didn't work as expected. The approach I'm using is based ...

Is there a way to customize the visual appearance of radio buttons by using unique color schemes

Incorporating bootstrap 4, I have implemented a basic set of custom yes/no radio buttons. <div class="custom-control custom-radio custom-control-inline"> <input type="radio" id="rd_1" name="rd" class="custom-control-input green" value="Yes"&g ...

Increment the name field automatically and track the number of auto-increment variables being sent through serialization

I am trying to implement a functionality where users can add more inputs by pressing a button, each representing an additional 'guest'. The issue I am facing is with auto-incrementing the JavaScript so that new inputs are added with an incremente ...

I am trying to connect HTML input with Python but I am not sure how to do it

As part of our capstone course team project, we are developing a self-hosted calendar app specifically designed for college students. One of our team members has taken the initiative to create HTML webpages for this self-hosted server. My current task is t ...

What is the best way to retrieve the chosen ID from a list box and pass it to a getJSON function within an ASP.Net MVC application?

I'm facing a challenge with my list box setup... @Html.ListBoxFor(model => model.SelectedCalendarId, Model.PeriodList) After setting up a jQuery call to my controller with a hardcoded value... var calId = 12; $.getJSON("/Publishing/GetLockState1 ...

What is the best way to retrieve the identity or specifics of the item that was right-clicked within the context menu

My AngularJS populated table includes a link button. When I right-click on this button, I've created a specific context menu using jQuery that pops up. The issue arises when I try to retrieve the ID of the item I clicked on in the context menu (such a ...

Issue: Adjustment of elements' size using an "onclick" method when reaching specific screen width

Seeking assistance with implementing media queries in JavaScript. Can an object be resized from JavaScript code based on a specific screen width, rather than the default one? For example, if I have a button that opens a div with a height of 600px when cli ...

Nest a Div inside another Div with a precise margin of 10 pixels

Recently, I attempted to create a fullscreen webpage like this one: https://i.sstatic.net/21KCr.png My goal was to have the color of each element change randomly every second. Here is the JavaScript code I implemented: <script> var tid = se ...

Can you explain the mechanics behind the animation of the upvote button on steemit.com?

Behold the upvote button of steemit.com: <span class="Icon chevron-up-circle" style="display: inline-block; width: 1.12rem; height: 1.12rem;"> <svg enable-background="new 0 0 33 33" version="1.1" viewBox="0 0 33 33" xml:space="preserve" xmlns=" ...

Material UI grid with 2 items: one aligned to the left and one aligned to the right in a single line

I'm struggling to understand how to effectively utilize Material UI's flexbox integration. I want to align items in a specific way, like this: export default function Home() { return ( <Grid container justify={"space-between&q ...

"Troubleshooting a problem with the value of a radio button

I am struggling with a form that contains three sets of radio buttons, each with values ranging from 0 to 3. I have JS/JQ code that is supposed to calculate the total points based on the selected radio buttons and display a specific response accordingly. H ...

Empty gap separating two side by side photos

Could someone assist me with removing the excessive white space between these two images? Each image is contained within its own respective div, with layer effects applied when hovered over. I've attempted changing the display to inline-block and sett ...

Can Chrome support color management for css colors?

In my current project, we are dealing with designers who work in Adobe RGB and are accustomed to viewing the vibrant colors from that spectrum in Illustrator. However, we are developing an application that will enable them to work in a web browser using a ...

ASP Mvc 3 does not have the ability to automatically bind from a JSON post request

I am facing an issue with my simple controller: public class UserCredentials { public string Username {get;set;} public string Password {get;set;} } public class AuthenticationController : Controller { public ActionResult Authenticate(UserCrede ...

Input the latest data into the Bootstrap form

Implementing the bootstrap4 framework in my web application, I have a table displaying various values. Each row includes an edit button at the end of the row (last column). The requirement is that when this edit button is clicked, a form should pop up with ...

Enhancing the appearance of dropdown menus for WooCommerce Variable products with custom CSS styling

I currently have a Wordpress website with WooCommerce and several variable products. Each product's variation has a dropdown menu that displays different options when clicked. My main concern is how to customize the appearance of these dropdown menus ...

Tips for displaying a div briefly before loading the second page

Incorporating a div named ADD, I aim to successfully load a second page within the current one using ajaxload. The challenge lies in displaying a div for 4 seconds before loading the second page. How can this be achieved? Following the wait period, the sec ...

PHP and AJAX combination login page: users may have to sign in twice when accessing on mobile devices

As a novice in the world of programming, I ventured into creating a basic login page - provided below is the simplified version devoid of any security measures. <input type="text" name="username"> <input type="password" name="password"> <in ...

Choosing children in jQuery can be tricky, especially when you want to avoid selecting the root element with the same name

I am encountering a challenge with my XML structure where some child elements share the same name as the root element. I am seeking a way to specifically select these child elements without including the root element itself. Despite searching through the j ...

Switch up the background hue of a multi-level dropdown menu in Bootstrap 5

Has anyone attempted to modify the background color in a multi-dropdown menu using Bootstrap 5? You can find the original code here. I am looking to change the background color for visited/active items in a multi-level dropdown menu. The code snippet belo ...