Tips on displaying a div next to an image icon using CSS and jQuery

I have a data table with an interesting feature - when you hover over the icon in the last column, it displays more information about that specific record. Here's how it looks:

ORIGINAL VIEW

However, I'd like to enhance this functionality by displaying a black background div next to the icon with detailed info every time you hover over the blue icon. Each icon should show different information. Below is the CSS code for this additional feature:

CSS

.info-extra {

     position: fixed;
     z-index: 999;
     background-color: black;
     color: white;
     width: 300px;
     height: 150px;
     border-radius: 5px;
     box-shadow: 3px 3px 3px gray;
     top: 50%;
     left: 50%;
  }

HTML

echo '<td><img src="Images/iconos/fatcow/FatCow_Icons16x16/information.png" onmouseover="mostrarInfo('.$extra.')" onmouseout="ocultarInfo('.$extra.')"></td>';
echo '<div id="extra-' . $extra . '" class="info-extra" style="display: none;">
           <p>Hola</p>
      </div>';

This is how the end result should look like:

EXPECTED VIEW

For the final touch, make sure the black background div aligns neatly next to the centered icon. How can this be achieved to work seamlessly with all icons? Any assistance would be greatly appreciated. Thank you.

Javascript

function mostrarInfo(numero) {

   $('#extra-' + numero).show();
}


function ocultarInfo(numero) {

   $('#extra-' + numero).hide();
}

Answer №1

Ensure that your $extra variable in the onmouseover function is enclosed in quotes to prevent it from being interpreted as a variable. Use escaped quotes (\") for this purpose. The div will then only display when hovering over the image. To center the div, set a height for each <td> element and position each div accordingly.

function showInfo(number, n) {
  $('#extra-' + number).css('top', (200 + n) + "px");
   $('#extra-' + number).show();
}


function hideInfo(number) {

   $('#extra-' + number).hide();
}
.info-extra {
     position: absolute;
     z-index: 999;
     background-color: black;
     color: white;
     width: 100px;
     height: 150px;
     border-radius: 5px;
     box-shadow: 3px 3px 3px gray;
     left: 0;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<table>
  <tr>
    <td>abcahtohaeth</td><td>defetathao3hg</td><td><img src="Images/iconos/fatcow/FatCow_Icons16x16/information.png" onmouseover="showInfo('a',20)" onmouseout="hideInfo('a')"></td>
  </tr>
  <tr>
    <td>abcahtohaeth</td><td>defetathao3hg</td><td><img src="Images/iconos/fatcow/FatCow_Icons16x16/information.png" onmouseover="showInfo('a',40)" onmouseout="hideInfo('a')"></td>
  </tr>
  <tr>
    <td>abcahtohaeth</td><td>defetathao3hg</td><td><img src="Images/iconos/fatcow/FatCow_Icons16x16/information.png" onmouseover="showInfo('a',60)" onmouseout="hideInfo('a')"></td>
  </tr>
</table><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="extra-a" class="info-extra" style="display: none;">
           <p>Hello</p>
      </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

Place picture in the bottom right corner of the div without using absolute positioning

I am looking for a way to position an image in the lower right corner of a div, but I want to avoid using absolute positioning. The reason for this is that when I use absolute positioning, the text wrap is affected due to the float right applied to the ima ...

Limiting Dojo dates to once a month on a specific day

Is there a way to restrict selection to only the first day of each month using a constraint? I want to disable all other dates except for the first day of the month, but I'm having trouble figuring out how to do this. I've been searching through ...

Ajax facilitates the loading of multiple containers

My goal is to utilize Ajax to load dynamic content. The process starts with a list of countries. When a country is selected, the cities in that country are displayed, followed by suburbs, companies, branches, and eventually branch details: Countries --> ...

Choosing Between PureFor and ES6 Map: A Comparison

I prefer writing Pure For(nested Fors) in JavaScript instead of using map because I have found it to be more efficient in terms of performance. Is this True or am I making a mistake repeatedly? If For is quicker, but Map is cleaner, which one should I cho ...

Unique design elements can be seen between emulators/cellphones and notebooks

My attempt to create a responsive design resembling a table using vh and vw units for height and width is facing some challenges. The goal was to have 5 rows and 3 columns. Initially, the design looked good on both mobile phones and notebooks with just two ...

I want to utilize an Ajax/JQuery filled dropdown to dynamically alter the user interface without the need for additional server-side requests

I am working on a feature where an AJAX post method populates a dropdown with object descriptions. But, I also need to update a textbox with the selected ID when changing the selection, without making another AJAX call. Here's the relevant code snipp ...

How can JavaScript be used to show a hidden element when a checkbox is selected?

I'm having trouble with a form on my website that has a dropdown box initially hidden using CSS (display: none;). I want to make this dropdown visible (display: inline;) when a checkbox from another element is checked. Can anyone suggest a solution fo ...

"Revolutionizing the Web: HTML5 Slogan Takes Center Stage in

Is there a way to create a document outline like this in HTML5 using semantic tags, while keeping the first two headings together? -MySite --Books for children ---Book1 ---Book2 Currently, my code looks like this: <body> <header class="INeedTh ...

Validate forms using Joi.js and receive error messages in JSON format

Is it possible to return a JSON object instead of an HTML page when using express-validation and Joi packages for validating forms on the server side? Currently, whenever an error occurs, it returns an HTML page as a response. Route file: // Validator co ...

Ways to replicate this impact

I came across a perfect match for what I need; it resembles a relationship user interface. Please check it out: How can I replicate that? I am looking to develop a PHP application that can retrieve relationships from a database and showcase them similarly ...

Manipulating the position of particles in a THREE.ParticleSystem using Three.js

I am faced with the challenge of manipulating multiple particle systems, each containing approximately 20,000 vertices. My goal is to individually move each particle within these systems to a new Vector3 location that corresponds to a specific shape I have ...

Angular JS: Distribute objects from a single array to various arrays or services

I have recently embarked on developing a basic app using Angular JS, utilizing a service/factory to manage data and enable the addition of objects. Within the array of various individuals (listed in the html), you can include them as candidates by employi ...

Toggle visibility of divs on button click

let loginBtn = document.getElementById("btn-login"); let loginDiv = document.getElementById("loglog"); let signupBtn = document.getElementById("btn-signup"); let signupDiv = document.getElementById("signMe"); loginBtn.addEventListener("click", () => { ...

Alter Text Using Typewriter

Recently, I have been experimenting with code on my glitch website, trying to create typewriter text. Thanks to help from a user on StackOverflow, I was able to achieve this effect successfully. However, I am now facing a new challenge. My goal is to make ...

During the AJAX request on the HTML page, additional elements are being added

I've implemented a piece of code that utilizes AJAX calls to refresh an HTML table every 5 seconds. The approach involves clearing out the content of the table and then appending all data once again every 10 seconds. Here's a snippet of the code ...

Wordpress AJAX request results in a value not found

** I have added more details, thank you for your assistance so far I am in the process of implementing an AJAX search functionality on my Wordpress site to filter properties based on type, location, and status. Being a novice at AJAX, I have been followi ...

Content Blocks with Added Cushioning

I am striving for the cleanest code possible. Can an article and an aside have padding, margins, etc? I have been struggling to achieve this. I find myself having to add a div with a class that accepts padding. article { width:463px; float:left; ...

How can you condense an if/else statement in JavaScript into one line?

When searching the DOM using $(current_form).find('input').data('option'), the result can either be undefined or a string of options like 'x|y|z'. To handle this, I typically split the string by calling split('|') to ...

What steps can I take to display a download button when a file is clicked on?

As a backend developer, I usually don't delve into JavaScript tasks, but I have a simple query for JavaScript developers that I need help with. Here is my question: I am trying to display a download button when a specific file is clicked using jQuery ...

Manipulate object position using Aframe and three.js

I'm working on developing a game using A-frame. I'm trying to add a shooting effect by incorporating a gun model that follows the user's cursor. I've coded a click event so that an object moves in front of the gun and follows the direct ...