Display an image when the link is hovered over within a table

I'm looking for a way to display a scanned receipt image when hovering over a link within a table. I've written some code but it's not working as expected. Can anyone help me figure out what's wrong?

Here is the current state of my code:

CODE

 jQuery(document).ready(function() {
   $(".preview").hover(function() {
     $(this).closest('img').show();
   }, function() {
     $(this).closest('img').hide();
   });
 });
 .hide-image {
   display: none;
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<tr>
  <td>01/14/16</td>
  <td>
    <a href="#" class="preview">Lowes Receipt
          <img src="image-path/image.jpg" class="hide-image" style="z-index: 100; position: absolute;" /></a>
  </td>
  <td>$1235.96</td>
</tr>

Answer №1

Is there a better way to accomplish this task without relying solely on CSS?

.hide-image {
  display: none;
  z-index: 100;
  position: absolute;
}
.preview:hover .hide-image {
  display: block
}
<tr>
  <td>01/14/16</td>
  <td>
    <a href="#" class="preview">Lowes Receipt
      <img src="//lorempixel.com/100/100" class="hide-image" /></a>
  </td>
  <td>$1235.96</td>
</tr>

Answer №2

$(document).ready(function() {
          $(".preview").hover(function() {
            $(this).find('img').fadeIn();
          }, function() {
            $(this).find('img').fadeOut();
          });
        });
.hide-image {
          display: none;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<tr>
  <td>01/14/16</td>
  <td>
    <a href="#" class="preview">Lowes Receipt
    <img src="https://via.placeholder.com/100" class="hide-image" style="z-index: 100; position: absolute;" />
    </a>
  </td>
  <td>$1235.96</td>
</tr>

Feel free to give this approach a try.

Answer №3

Consider using the next() method:

$(this).next('img').show();

Alternatively, you can use find() method:

$(this).find('img').show();

Answer №4

Moving up the DOM is key. I've learned from my past mistakes. Try using find("img");

Check out this demo: https://jsfiddle.net/unix102/yspbrwkd/

$(document).ready(function () {
    $(".preview").hover(function(){
        $(this).find('img').fadeIn();
     }, function(){
        $(this).find('img').fadeOut();
    });
});

Answer №5

The issue appears to be related to the functionality of jQuery's .show() method.

When using this method, the selected elements are instantly made visible without any animation. It is similar to setting .css( "display", "block"), but it also restores the original display property value.

Because the default display property value is set to none, calling show() has no effect.

Here's an alternative approach with jQuery while improving coding practices by moving some inline styles to a separate CSS file:

HTML

<tr>
   <td>01/14/16</td>
   <td>
      <a href="#" class="preview">Lowes Receipt
      <img src="image-path/image.jpg" class="receipt-image" /></a>
   </td>
   <td>$1235.96</td>
</tr>

CSS

.receipt-image {
    display: none;
    position: absolute;
    z-index: 100; 
}
.receipt-image.visible { 
    display: block;
}

Javascript

jQuery(document).ready(function () {
    $(".preview").hover(function(){
        $(this).closest('.receipt-image').addClass('visible');
    }, function(){
        $(this).closest('.receipt-image').removeClass('visible');
    });
});

However, achieving the same result solely with CSS is possible too, eliminating the need for jQuery or JavaScript! Remove the JS code and try out this CSS solution:

CSS

.receipt-image {
    display: none;
    position: absolute;
    z-index: 100; 
}
.preview:hover .receipt-image { 
    display: block;
}

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

The styled-components in CSS are causing some issues with the color themes

Link to Image Showing the Issue. I have implemented themes and colors in my React application successfully, but I am encountering a peculiar problem with the labels. Whenever I switch the theme from green to blue and then back to green, focusing on the inp ...

Unexpected '->' found on page during loading

After migrating my WordPress site from localhost to the live server, I encountered an issue where "-->" appeared before the page finished loading. Can anyone explain this strange behavior? In addition to this issue, the jQuery scripts I had implemented ...

Identifying the accurate folder for file uploads

In my project directory, I have a folder named uploads, along with two files called upload.html and upload.php. The relevant part of my upload.html file looks like this: $(document).ready(function(){ $("#pictureUploadSubmit").submit(function(event) { ...

Jest is unable to utilize Higher Order Components from JavaScript files, but it functions properly with Higher Order Components from TypeScript files

When I try to import an HOC from a tsx file, everything works fine. However, when I change the extension to js, Jest throws an error: Jest encountered an unexpected token. This usually indicates that you are attempting to import a file that Jest is unabl ...

HTML Tutorial: A Beginner's Guide to Invoking REST API

Hi there! I'm new to APIs and struggling to grasp the concept. The tutorials online seem more complex than necessary. Here's what I need help with: I want to create a basic webpage that allows me to search for information using the Pokemon API a ...

Utilizing a variety of Zend pagination controls through AJAX and jQuery technology

Looking for assistance in implementing multiple Zend pagination in a view using AJAX. Single pagination is already working, but now the goal is to have more than one. This is the current setup:- Added to the bootstrap:- public function _initPaginator(){ ...

Function cannot be executed through the onchange event handler

I am looking for a solution to track changes in the color of an option element and automatically change the color of the select element when an inactive environment is selected. For example, I want to mark inactive environments with the color #D4D4D4. Is t ...

JavaScript date formatting without using jQuery

Looking for help to apply a date mask (dd/mm/yyyy) on an ASP.net textbox. I have tried several JavaScript solutions found through Google search, but none seem to work seamlessly, especially with backspacing. Any suggestions for a reliable script would be ...

What is the best way to ensure the checkbox functions correctly in this specific situation?

I am utilizing PHP, Smarty, and jQuery in the development of my website. Currently, I am working on implementing checkboxes within a Smarty template. Below is a snippet of my Smarty code related to checkboxes: <table cellpadding="5" cellspacing="0" bor ...

What is the method to remove an authentication code from a JSON file using JavaScript?

Is there a way to delete something from a JSON file? Here is an example: The file I am working with is named test.json Before I delete the authentication code: { "auth": [ { "test": 944037 }, { "tester& ...

A comprehensive guide on troubleshooting the toggleComplete functionality for React Todo applications

When you click on an item in the to-do list, it should show a strikethrough to indicate completion. However, when I try clicking on an item, nothing happens. Here is my toggleComplete function and where I am attempting to implement it: class ToDoForm exten ...

How can a method inside an object property of the same class be invoked in JavaScript?

I'm faced with a bit of confusion here. I have a class property called hotspot of type object, declared as follows: Cannon.prototype.hotspot = {stuff: this.blah(...) }; The method blah() is actually a prototype of the same 'class': Canno ...

Attempting to store the output of a function in a variable

My current script is designed to verify if the value of a selected element matches the span id. While the function itself functions correctly (verifying object.id via alert), I am encountering issues with variable assignment. When attempting to alert the ...

The function has exceeded the time limit of 60000 milliseconds. Please make sure that the callback is completed

Exploring the capabilities of Cucumber with Protractor has been an intriguing journey for me. As I delved into creating a feature in Gherkin language, outlining the steps and scenarios necessary for my end-to-end tests, a new world of possibilities opened ...

Show an error message in a popup window, following a validation error in Laravel

I am facing an issue with displaying error messages in the update modal form. I am using Laravel request for validation and AJAX to submit the form inside a modal. My goal is to show the error message for each field that is inputted incorrectly. However, i ...

Tips for customizing scroll bar padding and margin using CSS classes or IDs

I am looking to customize three different types of scrollbars with unique padding and margins. Here is the global style for my scrollbars: /* Modifying the scroll bar across the whole application */ /* width */ ::-webkit-scrollbar { width: 5px; he ...

Struggling with eliminating the bottom margin on your website?

I can't figure out where this mysterious margin is coming from in the CSS - there's a consistent 30px of space at the bottom of each web page. Any assistance would be greatly appreciated. I know it's probably something simple, but my brain ...

Sending a JSON array from an Ajax request to a Spring controller: A step-by-step guide

My HTML table data is converted to JSON format and sent via AJAX to a Spring controller. In the controller, I used @RequestParam Map<String, String> to retrieve the values, but the entire JSON string was received as the only key. I cannot use a model ...

Obtain unique identifiers for the purpose of sorting the items

While utilizing Nuxt.js, I am fetching random images from URLs structured like this: http://www.randomimage.com?ID=myId To display 2 pictures, I use the following methods: getRandomArbitrary(min, max) { return this.numb = Math.floor(Math.random() * ...

How can I align an image and text alongside another image using HTML and CSS?

Having some trouble positioning an Image and Text next to another Image. Check out an example here: I attempted using floats, but it doesn't seem to be working. Here is the code I have: .left {float: left;} .right{float: right;} <div class="left ...