Encountering an issue with updating the image upon hovering over items within a list using Jquery

Whenever I attempt to choose a different item from the list, the image does not update. Upon inspecting with console.log, it appears that the selector remains static even when hovering over other items on the list, resulting in Image A always being displayed.

$(".list li a").mouseenter(function(){
  var currentImage = $(".honda").attr("data-cover");
  // console.log(currentImage)
  $(".honda").parent().prev().prev().find("img").attr("src", currentImage);
});
body {
  background-color: rgb(51, 51, 51);
}

.img-test {
  background-color: rgb(0, 230, 12);
  height: 100px;
  width: 200px;
  position: relative;
  margin-left: 300px;
  margin-top: 50px;
}

.list li {
  list-style: none;
  color: rgb(238, 238, 238);
  font-size: 25px;
  margin-top: 15px;
  padding: 10px;
  max-width: 500px;
  background-color: rgb(76, 102, 117);
  border: 1px solid rgb(155, 161, 180);
  cursor: pointer;
}

.list>li>a {
  color: rgb(238, 238, 238);
  text-decoration: none;
  display: block;
  padding: 10px;
  list-style: none;
  font-size: 25px;
}

.list li a:hover {
  background-color: rgba(194, 79, 188, 0.664);
  color: #ffcb6b;
  transition: all .4s ease;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="img-test"><img src="https://dummyimage.com/200x100/bfbfbf/000.jpg&text=Default+BG"></div>
<ul class="list">
  <li><a href="#">Image A</a></li>
  <li><a href="#">Image B</a></li>
  <li><a href="#">Image C</a></li>
</ul>
<div class="images">
  <div class="honda" data-cover="https://dummyimage.com/200x100/de2c2c/fff.jpg&text=A"></div>
  <div class="honda" data-cover="https://dummyimage.com/200x100/44679e/fff.jpg&text=B"></div>
  <div class="honda" data-cover="https://dummyimage.com/200x100/ffe554/000.jpg&text=C"></div>
</div>

I am trying to find a way for the selector to dynamically change as the mouse moves over an element in the list and for the corresponding image to update accordingly. Despite experimenting with various approaches, no significant progress has been made.

An image illustrating the issue has also been included.

The current outcome shows all three elements overlapping in the same location (shown on the left), whereas the expected behavior is depicted on the right.https://i.sstatic.net/QkW3R.png

Answer №1

Method 1: In case you're open to adjusting your code, here's a possible solution:

var $displayImage = $('.img-test img');

$(".list li a").on('mouseenter', function() {
  var currentImage = $(this).attr("data-cover");
  $displayImage.attr("src", currentImage);
});
body {
  background-color: rgb(51, 51, 51);
}

.img-test {
  background-color: rgb(0, 230, 12);
  height: 100px;
  width: 200px;
  position: relative;
  margin-left: 300px;
  margin-top: 50px;
}

.list li {
  list-style: none;
  color: rgb(238, 238, 238);
  font-size: 25px;
  margin-top: 15px;
  padding: 10px;
  max-width: 500px;
  background-color: rgb(76, 102, 117);
  border: 1px solid rgb(155, 161, 180);
  cursor: pointer;
}

.list>li>a {
  color: rgb(238, 238, 238);
  text-decoration: none;
  display: block;
  padding: 10px;
  list-style: none;
  font-size: 25px;
}

.list li a:hover {
  background-color: rgba(194, 79, 188, 0.664);
  color: #ffcb6b;
  transition: all .4s ease;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="img-test"><img src="https://dummyimage.com/200x100/bfbfbf/000.jpg&text=Default+BG"></div>
<ul class="list">
  <li><a href="#" data-cover="https://dummyimage.com/200x100/de2c2c/fff.jpg&text=A">Image A</a></li>
  <li><a href="#" data-cover="https://dummyimage.com/200x100/44679e/fff.jpg&text=B">Image B</a></li>
  <li><a href="#" data-cover="https://dummyimage.com/200x100/ffe554/000.jpg&text=C">Image C</a></li>
</ul>

Method 2: If you prefer not to change the existing code structure, consider this approach:

var $displayImage = $('.img-test img');

$(".list li a").on('mouseenter', function() {
  var index = $(this).parent().index();
  var currentImage = $(".honda").eq(index).attr("data-cover");

  $displayImage.attr("src", currentImage);
});
body {
  background-color: rgb(51, 51, 51);
}

.img-test {
  background-color: rgb(0, 230, 12);
  height: 100px;
  width: 200px;
  position: relative;
  margin-left: 300px;
  margin-top: 50px;
}

.list li {
  list-style: none;
  color: rgb(238, 238, 238);
  font-size: 25px;
  margin-top: 15px;
  padding: 10px;
  max-width: 500px;
  background-color: rgb(76, 102, 117);
  border: 1px solid rgb(155, 161, 180);
  cursor: pointer;
}

.list>li>a {
  color: rgb(238, 238, 238);
  text-decoration: none;
  display: block;
  padding: 10px;
  list-style: none;
  font-size: 25px;
}

.list li a:hover {
  background-color: rgba(194, 79, 188, 0.664);
  color: #ffcb6b;
  transition: all .4s ease;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="img-test"><img src="https://dummyimage.com/200x100/bfbfbf/000.jpg&text=Default+BG"></div>
<ul class="list">
  <li><a href="#">Image A</a></li>
  <li><a href="#">Image B</a></li>
  <li><a href="#">Image C</a></li>
</ul>
<div class="images">
  <div class="honda" data-cover="https://dummyimage.com/200x100/de2c2c/fff.jpg&text=A"></div>
  <div class="honda" data-cover="https://dummyimage.com/200x100/44679e/fff.jpg&text=B"></div>
  <div class="honda" data-cover="https://dummyimage.com/200x100/ffe554/000.jpg&text=C"></div>
</div>

Extra Tip: For improved performance, consider preloading the images.

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

How to position two divs next to each other using CSS with just one adjustment

Is it possible to position two divs side by side, with one of them moving continuously up and down while containing an image? I attempted the following code: <div> <div style="margin:30px;width:100px;height:250px;position:relative;float:left; ...

Is there a way to duplicate the background-style:contain effect on an image?

It is important to note that the image source must be declared in the HTML code as it will be dynamic, and therefore background cannot be used here. HTML <div class='some-form'> <form> <button>...<button> <i ...

Extracting data from the website chartink.com

I need assistance with extracting data from the following link: link - I have been attempting web scraping, but I am unable to retrieve the specific table that I require. Can someone please provide guidance on how to achieve this? Despite using the code ...

What is the method for placing a badge above a Font Awesome icon?

Is it possible to add a badge with numbers like 5, 10, or 100 on top of the Font Awesome symbol (fa-envelope)? I am looking for something like this: However, I am struggling to figure out how to place the badge on top of the symbol. You can see my attempt ...

Tips for creating a mobile-friendly responsive table

I need help with making my table responsive in mobile view. Can someone provide guidance on how to achieve this? Feel free to ask if you have any questions related to my issue. tabel.html I am currently using the Bootstrap framework to create the table ...

Enhancing CSS compatibility for Internet Explorer 9 with CSS3

I need to create a Jagged Edge border without relying on an image, and I want it to be compatible with IE9 and newer versions. Currently, it works in most browsers but not in IE9. Any suggestions or advice would be greatly appreciated. Thank you! Below is ...

What is the best way to display an image upon clicking a button?

I have a <button> element with the rel="example.jpg" attribute. The goal is to have the image load in the #area DIV only after the button is clicked, not during the page load. I have implemented the following code to achieve this: $(document).ready( ...

``There seems to be an issue with the alignment and vertical placement in both Firefox and IE

I'm currently working on a responsive landing page for a company and running into an issue with the CTA button's position outside of Chrome. It's functioning perfectly in Chrome, resizing as intended: Here is the CSS responsible for this: ...

Transitioning between sections by clicking on a navigation menu item

I'm looking to create a cool animation effect on my website. When a user clicks on a specific menu link, such as "about-section," I want the page to smoothly scroll down to that section in a parallax style. If anyone knows of a jQuery plugin that cou ...

Is there a way to incorporate a CSS file into this without explicitly mentioning the color?

I have successfully implemented a PHP solution for changing themes with a cookie that remembers the selected theme color when the user leaves the site. However, I now need to switch this functionality to JavaScript while still utilizing the CSS file. How c ...

The CSS within the WebComponent nesting is not valid

Upon testing, I have found that writing CSS directly using nesting is effective. However, when used within a WebComponent and nested with the :host selector, it becomes invalid. Below is a demo code snippet showcasing this issue. My Chrome version number i ...

Jquery for clearing multiple textboxes

I have several textboxes on a page that are dynamically generated and share the same ID. Currently, I am able to clear the content of the first textbox using jQuery as shown below: $('#commentText').val(''); However, this method only ...

utilize a modal button in Angular to showcase images

I am working on a project where I want to display images upon clicking a button. How can I set up the openModal() method to achieve this functionality? The images will be fetched from the assets directory and will change depending on the choice made (B1, ...

Starting the animation only when the slide is visible

Struggling to add animations dynamically as a HTML/CSS developer who avoids JS but uses jQuery occasionally? Sound familiar? Well, I have. Recently, I managed to create a CSS-3 animation for the images in a Bootstrap carousel. Check out the code below: ...

Just a snippet of a webpage shown

My website focuses on online magazines. I'm looking for a way to selectively display specific parts of articles that are in regular HTML format with a global CSS stylesheet applied. There are no images, only text. How can I extract certain segments of ...

Clicking on a hyperlink within an Angular Material button does not prompt the browser to navigate to the linked page

Currently, I am utilizing Angular Material with AngularJS v1. Encountering issues with a simplistic menu bar while utilizing angular material. Below is the piece of HTML code representing the menu bar: <md-toolbar layout="row" class="md-whiteframe-z3" ...

Combining these two statements in jQuery: What's the best way to do it?

if($(this).parents("ul").closest("#menu-main").length){ var parent = $(this).parent(); parent.addClass("current"); if(parent.hasClass("more-options")){ $(parent).siblings(".more-options-page").addClass("current").show(); } } Instead of redun ...

Displaying and Concealing Table Rows using Javascript

I am working with an array of prices that are being displayed in a table using a foreach loop. The goal is to hide specific rows in the table based on certain conditions. The $status variable is set to "YES" if the price is => 30, and "NO" if the price ...

How to Send jQuery ajax Requests with Multiple Buttons in a PHP Form

My current issue is as follows: <html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> </head> <body> <form id="roomform" action="room.php" method="POST"> <b ...

jQuery fails to function properly when using the .live("keyup") event

Below is the code snippet: $(document).ready(function() { $("#querybox").live("keyup", function(e) { var code = (e.keyCode ? e.keyCode : e.which); if (code == 13) { $("#querybox").blur(); } else { search(document.getElementB ...