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

Swiper.IO pagination indicators not displaying

Why is the pagination not showing up on the image carousel I created with Swiper? Despite being in the DOM, it has a height of 0 and manual changes have no effect. Any suggestions? import Swiper from '../../vendor/swiper.min.js'; export default ...

Is it possible to retrieve the values of #select1 and #select2 before initiating the AJAX request?

I am presented with the following snippet of HTML code: <select id="choice1"> <option value="0">Option 0</option> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3 ...

How can we use jQuery to extract an HTML element's external stylesheet and add it to its "style" attribute?

My goal is to extract all CSS references from an external stylesheet, such as <link rel="stylesheet" href="css/General.css">, and add them to the existing styling of each HTML element on my page (converting all CSS to inline). The reason for this re ...

Creating a flexible grid layout with DIVs that share equal width dimensions

Struggling with my HTML and CSS code, I need a way to structure nested DIV blocks into a responsive grid where each block has the same width. Despite my efforts, nothing seems to work as expected. Currently, the nested DIVs are stacked vertically one on to ...

Provide a unique <li> attribute for the JavaScript function to utilize

Is there a way to pass specific attributes from dropdown options to a javascript function? I have tried using .data() and .attr(), but the console keeps showing "undefined". Any suggestions on how to achieve this in a cleaner and simpler way would be gre ...

A dynamically-generated dropdown element in the DOM is being duplicated due to JavaScript manipulation

Currently, I am dynamically adding a dropdown element to the page using javascript. The goal is for this dropdown to modify the properties displayed on a map. However, when attempting to add the element after the map loads, I encounter an issue where the d ...

Creating a dynamic slider using jQuery

Here is the code snippet I have been working on: var current_slide = 1, animation_time = 1000, pause = 3000, slide_container = $('.slide_container'), interval, height = slide_container.height(), slides ...

Ways to incorporate CSS design into Django input pop-up when the input is invalid

Looking to enhance the error message styling for a Django Form CharField when an incorrect length is entered, using CSS (Bootstrap classes preferred). I have successfully styled the text input itself (check the attrs section in the code), but I am unsure ...

Checking for app installation using jQuery AJAX across different origins

I am looking for a way to verify if a user has my app installed by using the protocol 'myapp://.' When a button is clicked, I want to check if the app is installed on the user's device. If it is, I would like to redirect the browser to the ...

Elements that intersect in a site's adaptable layout

I need to create a design similar to this using HTML/CSS with the Skeleton framework: view design preview I've experimented with various methods to overlap the background and image, utilizing absolute positioning for the image. However, this approach ...

How can I achieve auto-refresh functionality in SQLite using PHP and JavaScript without refreshing the entire page

I am currently working with a straightforward php code that retrieves data from a SQLite database by using the query "$query ="Select A from B"". The process works smoothly, and upon updating the SQLite database, I can refresh the page to display the new d ...

I am using jQuery Ajax and PHP to receive data in JSON format

Hey there, I've been trying to retrieve a 2-dimensional array from PHP using Ajax and jQuery, but I'm having trouble getting a response. Here's the code I have: HTML code $.ajax({ type: "POST", url: "get_data.php", data: "", ...

Automatically updating the JavaScript content on a webpage without having to refresh the entire HTML page, based on the changing

Is there a way to adjust the opacity change rate of my nav bar automatically without the need to refresh the page? The current opacity change rate is based on the width of the window, but once the page is loaded, adjusting the width does not affect the rat ...

Reinitialize Yii2 form with jQuery without triggering mandatory data warning

I've tried multiple ways to reset the form, but none seem to work correctly. In Yii2, I keep getting a "data required" error even after clearing a field, and one of them doesn't even clear. Here is the Yii2 action function: public function acti ...

What is the process of transferring information from one window to another window?

Is there a way to transfer data between two windows? In my parent window, I have a button that redirects to another page with a success button. When the success button is clicked on the child window, I want to display "success" text on the parent window. ...

Exploring the fundamentals of Django with a touch of creativity in CSS

As a newcomer to Django, I am currently working on setting up a basic Django application. I have progressed to chapter 5 in the Django online book: Before delving into databases, my goal is to incorporate some simple CSS and JS directly into the applicat ...

What is the functionality of background attachment fixed?

I am curious about how background images behave when the background-attachment property is set to fixed. Here are my questions: If I set a background image for a div with dimensions of 500px by 500px and add a 1px solid red border, then apply backgro ...

Generate an interactive rain accumulation graph by combining multiple data points of rainfall into a dynamic Highcharts chart

I am looking to create a highchart that displays rain data along with the accumulated rain for the visible data points in the chart (where the accumulated rain series will always remain at 0), similar to this example image: To populate the static series f ...

JQuery failing to display image within set timeframe

In my website, I have implemented a feature that displays data from a database using auto-scrolling. As the user scrolls down the page, an AJAX function is triggered to fetch the next set of records from the database and append them to the existing content ...

Ensuring continuous execution of JS EventListener

The other day, I received a solution from someone on this platform for a script that changes the color of a div when scrolling. Here is the code I implemented: const x = document.getElementById("menuID"); window.addEventListener("scroll", () => { ...