Switch the image source when hovering over a text menu

Currently, I have been using the following code to switch between images. However, what I actually want to do is change the image when hovering over the title link of the image.

onmouseover="this.src='Images/img.png'" onmouseout="this.src='Images/img2.png'"

For instance, when someone hovers over the title link such as 'Chicken Menu', the image below it will transition from img.png to img2.png. In this scenario, img.png represents the front page image and img2.png showcases a list of menu items.

Answer №1

To achieve this effect using only CSS, you can utilize the adjacent sibling combinator (+).

Let's consider your HTML structure:

<div id="headingtext">HEADING</div>
<div id="imgDiv"></div>

The corresponding CSS would be:

#headingtext:hover + #imgDiv {
    background-image: url("https://images.blogthings.com/thesingleflowertest/flower-3.png");
    background-repeat: no-repeat;
    height: 500px;
}

#imgDiv {
    background-image: url("https://images.blogthings.com/thesingleflowertest/flower-5.png");
    background-repeat: no-repeat;
    height: 500px;
}

Here is a Fiddle Demo showcasing this implementation: http://jsfiddle.net/3z8CN/

Answer №2

One way to achieve this is by using the following code:

<a href="#">Link</a>
<img src="http://www.blogwmp.com/wp-content/uploads/2009/08/google-logo-100x100.png" alt="">

<script>
    $('a').hover(function(){
        $('img').attr("src", "http://www.betaarchive.com/forum/download/file.php?avatar=16617_1318156786.png");             
    }, function(){
        $('img').attr("src", "http://www.blogwmp.com/wp-content/uploads/2009/08/google-logo-100x100.png");             
    });
</script>

You can view a live demo of this code here

By using .hover(), you are able to dynamically change the image source attribute when hovering over the link, and revert it back to the original image when the mouse leaves the link.

Answer №3

Take a look at the demonstration on the fiddle...

View the Fiddle Here

Make sure to include jQuery in your code.

--HTML--

<ul>
    <li>
        <span class='my_span'>Here is an image</span>
        <img src="http://dummyimage.com/100x100/000000/fff"/>
    </li>
    <li>
        <span class='my_span'>Here is another image</span>
        <img src="http://dummyimage.com/100x100/000000/fff"/>
    </li>
</ul>

--jQuery--

$('span.my_span').hover(
    function(){
        $(this).next('img').attr("src","http://dummyimage.com/100x100/888888/000");
    },
    function(){
        $(this).next('img').attr("src", "http://dummyimage.com/100x100/000000/fff");             
    }
);

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

Maximizing the efficiency of a personalized hook that facilitates data sharing in React

I have developed a unique Custom Hook that looks like the following: import { useEffect, useState } from 'react'; import axios from 'axios'; const myCustomHook = () => { const [countries, setCountries] = useState([]); const [i ...

Storing information upon refresh in Angular 8

When it comes to inter-component communication in my Angular project, I am utilizing BehaviourSubject from RXJS. Currently, I have a setup with 3 components: Inquiry Form Where users enter an ID number to check for summon-related information. This data ...

Guide on keeping a footer anchored at the bottom of a webpage

I have gone through numerous tutorials on how to position the footer at the bottom of my webpage, but I'm still struggling to accomplish it for my own site. Some of the resources I've consulted include: Understanding techniques to keep the fo ...

Spotting the Visible Element; Detecting the Element in

Currently, my webpage has a fixed header and I am facing the challenge of dynamically changing the styling of the li elements within the navigation based on the user's scrolling position. To achieve this, I believe the most effective approach would b ...

Tips for setting up OpenLayers demonstrations on a personal server?

Currently, I'm working with an older version of OpenLayers (4.6.2) and find the provided examples extremely helpful for testing and reference purposes. However, the official web page with updated examples only includes the latest version. I am wonder ...

I'm not sure where to set the timeout for automatically refreshing the page

Trying to implement an auto-refresh feature on my page to fetch the most recent data from a database. I am anticipating that this will display the latest information without requiring any manual action, but unfortunately, new inputted data is not reflect ...

Tips for filling jstree with information in Grails

I am currently facing difficulties in populating a jstree within a Grails gsp. Here is what I have attempted so far (rewritten for clarity): <script> $("#treeView").jsTree(); </script> <div id="treeView"> <g:each in="${atomMa ...

What causes the selected option to be hidden in React?

I created a basic form demo using react material, featuring only one select field. I followed this link to set up the select options: https://material-ui.com/demos/selects/ With the help of the API, I managed to display the label at the top (by using shri ...

The functionality of triggers is not applicable to content that is dynamically loaded through ajax requests

I encountered a problem recently that required me to rewrite some functions. The original code looked like this: $('#type_of').on('change', function () { However, due to issues with manipulating the DOM and loading content via ajax, I ...

Looking for a simple method to generate a text-only dropdown link in Bootstrap 4 without using a button?

Is there a simple method to create a text-only dropdown link in Bootstrap 4 without using a button? Or do I have to adjust the padding and buttons using CSS to make the "Delivery" dropdown blend in with the rest of the text? I've searched online and ...

Determine the height of an element in JavaScript or jQuery by using the CSS property height: 0;

I'm facing a challenge in determining the actual height of an element, which currently has a CSS height property set to: height: 0; When I check the console, it shows a height of 0, but I'm looking to find the real height of the element. I als ...

The browser sends a request for a file, the server then downloads a pdf, and finally, the

Here is the process I am trying to achieve: 1) A browser sends an ajax request to the server, requesting a pdf file. 2) The server downloads the pdf file and returns it for display. 3) The browser then displays the downloaded pdf in an existing iframe. Be ...

What is the reason for the error that Express-handlebars is showing, stating that the engine

I recently added express-handlebars to my project and attempted the following setup: const express = require("express"); const exphbs = require('express-handlebars'); const app = express(); app.engine('.hbs', engine({defaultL ...

How can I transform a JSON object into a series of nested form fields?

Can anyone suggest a reliable method for converting a JSON object to nested form fields? Let's consider the following JSON object: {'a':{'b':{'c':'1200'}}}, 'z':'foo', 'bar':{&apo ...

When attempting to pass data to another page by clicking on a row, the result is that the data appears to be empty

I have been using the MUI-Datatable component, which is able to navigate to the next page. However, when I try to view the data on the History page, nothing appears. How can I resolve this issue? Data transfer to another page: Even though I can see the d ...

Utilizing PHP, Javascript, and jQuery in mobile technology gadgets

Can anyone recommend mobile technology products that have been developed using PHP, Javascript, and jQuery? What are the newest mobile products available on the market that have been built using these languages? Do popular devices like iPhone, BlackBerry ...

Unable to display Apexcharts bar chart in Vue.js application

Struggling to incorporate Apexcharts into my Vue.js site, but unfortunately, the chart isn't appearing as expected. -For more guidance on Apexcharts, check out their documentation Here HTML <div class="section sec2"> <div id="chart" ...

Is there a way to identify which elements are currently within the visible viewport?

I have come across solutions on how to determine if a specific element is within the viewport, but I am interested in knowing which elements are currently visible in the viewport among all elements. One approach would be to iterate through all DOM elements ...

When attempting to use the ResponsiveSlides plugin, the functionality of 'Image links' seems to be disabled in the Chrome browser

While everything is functioning perfectly in Firefox and IE, I've encountered an issue with Chrome. It only works after right-clicking and inspecting the element; once I close the Inspector, it stops working - displaying just an arrow cursor as if the ...

Resources for Vue.js stylesheets

Vue.js is my latest discovery and I have been experimenting with the single file component architecture. A small issue I encountered was that all of my components' styles were being loaded on the page, even if they weren't currently active. Is t ...