Display an image when hovering over a list in HTML, utilizing JS and JQuery

When I hover over the text, I want those images to become visible. However, it seems that only the first image is selected because they all have the same ID. Does anyone know how to solve this issue? This is my first post, so please forgive me if I made any mistakes in posting. Thank you in advance.

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>jQuery UI Selectable - Default functionality</title>
        <link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">

        <script type = "text/javascript" src = "js/jquery.js"></script>
        <script type = "text/javascript" src = "js/jquery_ui.js"></script>

        <link rel="stylesheet" href="/resources/demos/style.css">

        <style>
            #feedback { font-size: 1.4em; }
            #selectable .ui-selecting { background: #FECA40; }
            #selectable .ui-selected { background: #F39814; color: white; }
            #selectable { list-style-type: none; margin: 0; padding: 0; width: 20%; }
            #selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }

            img
            {
                position:absolute;
                left:250px;
                display:none;
            }
        </style>

        <script>        
            $(function() {
                $( "#selectable" ).selectable();
            });
        </script>
    </head>

    <body>

        <table id="myTable">
            <td>
                <tr>
                    <ol id="selectable" onmouseover="show(next,true)" onmouseout="show(next,false)">
                        <li>Item 1 <img src="next.jpg" id="next"></li>
                        <li>Item 2 <img src="next.jpg" id="next"></li>
                        <li>Item 3 <img src="next.jpg" id="next"></li>
                        <li>Item 4 <img src="next.jpg" id="next"></li>
                        <li>Item 5 <img src="next.jpg" id="next"></li>
                    </ol>
                </tr>
            </td>
        </table>

        <script type = "text/javascript"> 
            $(document).ready(function() {
                $('#selectable').fadeIn('very slow');
            });
        </script>

        <script language="javascript">
        //function to display the immage
            function show(id,disp) {
                if (disp == true) {
                    id.style.display = "block";
                }

                if (disp == false) {
                    id.style.display = "none";
                }
            }
        </script>
    </body>
</html>

Answer №1

Ensure to change IDs when processing, since you are using IDs for reference

<li>Item 1 <img src="next.jpg" id="newID1" ></li>
<li>Item 2 <img src="next.jpg" id="newID2" ></li>
<li>Item 3 <img src="next.jpg" id="newID3" ></li>
<li>Item 4 <img src="next.jpg" id="newID4" ></li>
<li>Item 5 <img src="next.jpg" id="newID5" ></li>

Answer №2

It is important to ensure that the id of elements are unique. Instead, you can use the same class for similar elements and then pass the class name in the show function.

Update: It is recommended to have the mouseover and mouseout events on individual list items (li) rather than on the entire ordered list (ol) to target specific elements.

<ol id="selectable">
  <li>Item 1 <img src="next.jpg" class="next"  onmouseover="show(this,true)" onmouseout="show(next,false)"></li>
    <li>Item 2 <img src="next.jpg" class="next"  onmouseover="show(this,true)" onmouseout="show(next,false)"></li>
    <li>Item 3 <img src="next.jpg" class="next"  onmouseover="show(this,true)" onmouseout="show(next,false)"></li>
    <li>Item 4 <img src="next.jpg" class="next"  onmouseover="show(this,true)" onmouseout="show(next,false)"></li>
    <li>Item 5 <img src="next.jpg" class="next"  onmouseover="show(this,true)" onmouseout="show(next,false)"></li>
</ol>

Your show function:

function show(clickedObj,disp) {
    if (disp == true) {
        clickedObj.style.display = "block";
    }

    if (disp == false) {
        clickedObj.style.display = "none";
    }
}

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

Press the button to duplicate the cell column separated by commas

In my HTML table, there is a specific column filled with container numbers that I want to copy to the clipboard when a button is clicked. The column has the class ".container" Here is the code I have been working on. HTML: <button onclick="copyToC ...

Employing Jquery for restricting checkbox selection based on parent HTML elements

UPDATE I am looking to limit checkbox selection in specific sections on the same page. To prevent conflicting inputs from different sections, I believe using the label selector <label data-addon-name="xxx"> as a separator is the best appro ...

The vertical scrolling functionality of the MUI DataGrid in Safari may occasionally fail to work

Utilizing the <Box> component from MUI core and the <DataGrid> component from MUIX, along with some other components, I have created a data grid that has the following appearance: https://i.sstatic.net/Gc8sP.png When the number of rows exceed ...

Create a full-width slider using Materialize CSS framework

When using materializecss to create a slider, I encountered an issue where the image is full width but not full height, causing scrollbars to appear. What changes can I make to ensure the slider fills out my screen without any scrollbars? Additionally, I ...

How to retrieve the values of a property from a JavaScript object

Looking for a fresh perspective on this issue. I'm currently retrieving historical data from the cryptocompare API, but I'm having trouble accessing the received values. https://i.sstatic.net/oH6hz.png When I hardcode it as res.data.OMG.USD, I ...

The AngularJS error message states that there is an issue because the $resource function is

I am currently facing an issue with my controller, specifically the error message: TypeError: $resource is not a function This error is pointing to the line var Activities = $resource('/api/activities'); app.controller('AddActivityCtrl& ...

Troubleshooting JavaScript Integration in PHP Scripts

I'm currently working on creating an advertisement switcher that can display different ads based on whether the user is on mobile or desktop. I've tried inserting PHP includes directly into the code, and while it works fine, I'm struggling t ...

Is it possible to utilize an API response within a conditional statement in NextJS?

I am working on a change password feature that interacts with an API for verification. If the current password entered is incorrect, I want to display an error message. If you have any suggestions on how to proceed or if there are any flaws in my approach ...

Basic use of AJAX for sending the value from jQuery's datepicker

As a novice in JavaScript and AJAX, I'm hoping for your patience as I navigate my way through. In the scope of this project, I am utilizing the CodeIgniter framework. My objective is to implement a datepicker and transmit its value via AJAX. Below is ...

Send a JavaScript object to an MVC controller using an HTTP POST request

I'm encountering an issue while attempting to send a JavaScript object containing a string and a jstring to an MVC controller. Despite my code looking correct, I am receiving a null value in the controller. Any assistance would be greatly appreciated. ...

Vue.js - Launching Modal for Editing Data Entry

I have a list of records with corresponding buttons on the right side, as shown in this image: https://i.sstatic.net/uevzR.jpg Whenever I click on one of these buttons, I want a dialog box to appear containing various input fields such as text inputs, dro ...

Incorporating jQuery Masonry for seamless overlapping effect while implementing infinite scroll

I've developed a script that enables infinite scrolling on my website: $(document).ready(function() { function getLastId() { var lastID = $(".element:last").attr("id"); $.post("2HB.php?action=get&id=" + lastID, ...

The process of transmitting messages back and forth between a popup script and a content script

I am in the process of developing a Chrome extension that involves sending data requests from a popup script to a content script (via a background script), analyzing the request on the content script, and then sending back a response (again through the bac ...

Implementing Singletons in Node.js

While examining some code, I came across the following snippet: var Log = require('log'); // This creates a singleton instance module.exports = new Log('info'); Initially, my reaction was doubting that it could be considered a single ...

Modifying ng-model after file selection in a directive

I have a unique directive that is able to detect changes in an input field of type file. When this change occurs, my goal is to then locate another input field and modify its ng-model. The issue I am facing is that I am unable to trigger the model change s ...

Upon attempting to utilize Socket.io with Next.JS custom server, the server repeatedly restarts due to the error message "address already in

Every time I attempt to execute the refreshStock() function within an API endpoint /api/seller/deactivate, I encounter the following error: Error: listen EADDRINUSE: address already in use :::3000 at Server.setupListenHandle [as _listen2] (net.js:1318: ...

Hierarchy-based state forwarding within React components

As I embark on the journey of learning Typescript+React in a professional environment, transitioning from working with technologies like CoffeeScript, Backbone, and Marionettejs, a question arises regarding the best approach to managing hierarchical views ...

Tips for merging jquery-1.10.2.js and jQuery-2.1.4.min.js simultaneously

Hey there! I've run into an issue where including jQuery-2.1.4.min.js and jquery-1.10.2.js together is causing the following error: custom.js:375 Uncaught TypeError: $(...).autocomplete is not a function If I remove jQuery-2.1.4.min.js, then I enc ...

Amusing antics observed when hovering over a div in Firefox

Issue resolved, many thanks to all Here is the dilemma I am dealing with something like this <a href='http://google.com' class="buy_now" > <div class='yada yada' > Go </div> </a> buy_now changes backgro ...

What is the reason JSON.parse fails to convert a string into a JSON object?

I attempted to convert a string into a JavaScript object using JSON.parse, however, it seems to be unsuccessful. After trying various methods, I did not receive any output in the console.log and no error messages were displayed. JSON.parse(`{'exp&apo ...