When using jQuery, you can utilize the mouse over event to display elements with the same class within an each loop

I am struggling with a hover effect on my webpage. I have three visible elements and three hidden elements, and I want each visible element to display only one hidden element when hovered over. However, the issue is that currently, hovering over a visible element reveals all three hidden elements instead of just one. I attempted to use the jQuery each function in my code but it seems to be ineffective. Can someone help me identify what I am doing wrong? Below is the snippet of my code:

$(document).ready(function(){

    $( ".thumbnail-top" ).each(function() {
        $( this ).mouseover(function(){
      $(".productImageBox-1").fadeIn('400');
      });
      $( this ).mouseout(function(){
        $(".productImageBox-1").fadeOut('400');
        });

      });

});
.thumbnail-top {
position:relative;
float:left;
margin:5px;
}

.product-thumbnail {
width:200px;
height:100px;
display:block;
padding:1rem;
color:#fff;
text-align:center;
cursor:pointer;
}

.productImageBox-0 {
background:blue;
}
.productImageBox-1 {
background:red;
position:absolute;
top:0;
left:0;
z-index:1;
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="thumbnail-top">
<a class="product-thumbnail productImageBox-0">I AM A BLUE BOX</a>
<a class="product-thumbnail productImageBox-1">NOW I AM A RED BOX</a>
</div>
</div>

<div class="thumbnail-top">
<a class="product-thumbnail productImageBox-0">I AM A BLUE BOX</a>
<a class="product-thumbnail productImageBox-1">NOW I AM A RED BOX</a>
</div>
</div>

<div class="thumbnail-top">
<a class="product-thumbnail productImageBox-0">I AM A BLUE BOX</a>
<a class="product-thumbnail productImageBox-1">NOW I AM A RED BOX</a>
</div>
</div>

Answer №1

To achieve the desired outcome, make sure to confine the selection within the children of $(this). Using mouseenter and mouseleave events may better suit your requirements.

$(document).ready(function() {

  $(".thumbnail-top").each(function() {
    $(this).mouseenter(function() {
      $(this).find(".productImageBox-1").fadeIn('400');
    });
    $(this).mouseleave(function() {
      $(this).find(".productImageBox-1").fadeOut('400');
    });

  });

});
.thumbnail-top {
  position: relative;
  float: left;
  margin: 5px;
}

.product-thumbnail {
  width: 200px;
  height: 100px;
  display: block;
  padding: 1rem;
  color: #fff;
  text-align: center;
  cursor: pointer;
}

.productImageBox-0 {
  background: blue;
}

.productImageBox-1 {
  background: red;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="thumbnail-top">
  <a class="product-thumbnail productImageBox-0">I AM A BLUE BOX</a>
  <a class="product-thumbnail productImageBox-1">NOW I AM A RED BOX</a>
</div>


<div class="thumbnail-top">
  <a class="product-thumbnail productImageBox-0">I AM A BLUE BOX</a>
  <a class="product-thumbnail productImageBox-1">NOW I AM A RED BOX</a>
</div>


<div class="thumbnail-top">
  <a class="product-thumbnail productImageBox-0">I AM A BLUE BOX</a>
  <a class="product-thumbnail productImageBox-1">NOW I AM A RED BOX</a>
</div>

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

Disable other actions during jquery change event validation

Is there an answer for this question already? If so, I apologize! Here is my situation: I have an edit field that is validated when the .change() event occurs. What I am trying to achieve is this: if a user types something and then immediately clicks the s ...

Please provide instructions on how to update a webpage section using ajax/json

Currently, I am in the process of developing a chat website and focusing on managing ONLINE USERS. I have implemented AJAX to handle refreshing data, however, I am facing issues with using Append(); method. Whenever I refresh the section, the same data k ...

Puzzled by the CSS Box Model - There's a piece of the puzzle I

Check out my simplified fluid layout right here at this link. I've been trying to figure out why the alignment between the right column and the header above it seems off, even though there's no padding involved. Both have the same border styles, ...

Turn off the ability to cache an HTML file without using PHP

I am currently faced with the challenge of managing a rather outdated, small website stored on a web space that does not support PHP. To overcome this limitation, I have decided to use iframes to avoid having to manually implement the basic structure and m ...

Dynamic calendar with flexible pricing options displayed within each cell

I've been wracking my brain over this issue for quite some time now, but still can't seem to find a solution! Is there a React Calendar out there that allows for adding prices within the cells? I simply want to show a basic calendar where each c ...

What is the best way to have a div gradually glide out (utilizing CSS's transition feature) upon the click of a particular button?

I want the hint-bubble div to smoothly slide out (using 'transition: 0.5s') whenever the hint-btn is clicked. I have successfully implemented it so that the hint-bubble appears when the button is clicked, but it appears instantly and I am struggl ...

ASP.NET Core MVC: Issue with Passing C# Razor Variable to HTML Tag

GitHub Repo: https://github.com/shadowwolf123987/Gun-Identification-App I'm currently facing an issue in passing the values of two C# variables from the code block defined in my view to the corresponding html tags within the same view. Unfortunately, ...

How can I use JavaScript to sort through an array and organize the data into groups?

Below is an array that I currently have: Status=["active","inactive","pending","active","completed","cancelled","active","completed"] I am looking to achieve the following result: StatusInfo=["active":3,"inactive":2,"pending":1, "completed":2, "cancelle ...

Deleting a segment of code in HTML with Python

My HTML text is lengthy and follows this structure: <div> <div> <p>Paragraph 1 Lorem ipsum dolor... long text... </p> <p>Paragraph 2 Lorem ipsum dolor... long text... </p> <p>Paragraph ...

Enable the choice for Bootstrap collapse animation to be customized

Is there a way to allow users or admins on my website to decide whether or not Bootstrap 4 collapse elements should be animated? The concern is that when many elements are moved by the animation, it becomes less smooth. Therefore, it would be ideal to give ...

Quicker loading times for background images when creating PDFs using wkhtmltopdf

As I work on generating a 5-page PDF from my HTML file using wkhtmltopdf, everything seems to be running smoothly. However, I've encountered an issue when it comes to the time it takes to complete this task, especially when a background image is inclu ...

Error message: The object does not have the necessary support for the property or method 'modal'

When attempting to display a modal pop-up to showcase details of a selected record in a grid, I encounter an issue. Despite setting the values for each control in the modal pop-up, it fails to open. I have ensured that all necessary references are included ...

Simple HTML files on a local server are having trouble loading images and JavaScript, yet the CSS is loading

Having worked in web design for quite some time, I recently began working on a basic Angular app that is meant to be very simple. For this project, I am only using the angular files and a single html file as the foundation. As I started setting up the bas ...

Preventing default events and continuing with jQuery

Currently, I am working on a Django project and have integrated the Django admin along with jQuery to insert a modal dialog between the submission button and the actual form submission process. To accomplish this, I have included the following code snippe ...

Guide to placing a basic div directly over a basic html table td

I need to create a simple html table with labels in one column and content in another. What I want to achieve is placing a div over the content td while keeping the label visible. I am looking for the most efficient way to do this, possibly making the seco ...

What is the best way to determine which id has been clicked using jQuery?

Can someone help me figure out how to determine which button has been clicked using jQuery? Here is the HTML code I am working with: <div class="row"> <div class="col-md-6"> <div class="well " id="option_a"> </div& ...

Arrange one div on top of another div using Bootstrap

Could someone please show me how to position a div above another div? Here is the desired layout: https://i.sstatic.net/mpeue.png <div class="col-md-12 container"> <div class="col-md-12"> box1 </div> <div class="col-md-12" ...

Refresh Details View with Ajax更新

I have implemented MVC3 Jquery and AJAX to create a List/Details view. Below is an excerpt of the code I am using: $(document).ready(function () { $('#example tbody tr').click(function () { alert('row was clicked'); ...

Ways to ensure that the ul navigation bar spans the entire bottom of the page evenly (without resorting to using table

This question has been previously raised on Stack Overflow with an insightful answer provided. However, I am encountering a specific problem in implementing it. My objective is to have a navigation bar (nav consisting of ul and its li items) occupy the en ...

The function $(...) does not recognize tablesorter

Currently, I am encountering issues with the tablesorter plugin as my system is unable to recognize the existing function. It is unclear whether there might be a conflict with other JavaScript files, especially since I am implementing changes within a Word ...