Modify the displayed image when hovering using inline coding techniques

I am having trouble changing an image on hover. I have attempted various methods without success. However, I need to stick with something similar to this code. Please let me know if there are any issues with this code. Thank you in advance.

Code

    <a href = "https://my-website.com/results/testimonials-global-supply-chain-group/">
    <img id = "home-button" alt = "" src = "https://my-website.com/wp-content/uploads/2017/11/businessman-2606509_1920_20171107090703313.jpg" 
     onmouseover="$(this).attr('src','https://my-website.com/wp-content/uploads/2017/10/testimonialpage.jpg');" 
     onmouseout="$(this).attr('src','https://my-website.com/wp-content/uploads/2017/10/businessman-2606509_1920_20171107090703313.jpg');">
    </a>

Answer №1

Before proceeding, make sure to verify that jQuery is included and inspect the console for any errors. Additionally, it seems like you may be overcomplicating your code when a simpler solution exists. You can simply use this.src:

<a href="#">
  <img src="//placehold.it/100?text=Hello"
       onmouseover="this.src='//placehold.it/100?text=Over'"
       onmouseout="this.src='//placehold.it/100?text=Hello'"
       alt=""
  />
</a>

Things to Check:

  1. Check the browser console for errors.
  2. Confirm if jQuery has been properly loaded.
  3. Verify that the image path is accurate.

Answer №2

If you're looking to add a hover effect to an image, there's a couple of ways you can achieve this. One option is using jquery, but for a simpler solution, you can stick with CSS. All you need is the :hover selector to change the background-image property upon hovering over the image. Check out this example. Here's the code snippet:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Alter an image on mouse hover</title>
        <style>
            .image {
                width: 130px;
                height: 195px;
                clip-path: border-box;
                background: url(https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.drodd.com%2Fimages14%2Fred4.jpg&f=1&nofb=1) no-repeat;
            }
            .image:hover{
                background: url(https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.zAvnUSUdzPkfFVXmUxbBpQHaE6%26pid%3DApi&f=1) no-repeat;
            }
            body {
                font-family: sans-serif;
                text-align: center;
            }
 
        </style>
    </head>
    <body>
<div class="image" style="margin-left: auto; margin-right: auto;"></div>
    </body>
</html>

Answer №3

If you are looking to dynamically alter image on hover, one efficient way is by utilizing CSS.

Alternatively, if you prefer to achieve this at runtime, you can refer to the methods outlined in the following post: Changing CSS properties on hover using JavaScript

Answer №4

Give this a try, it should do the trick.

Here's how you can create a hover effect using jQuery:

function addHoverEffect(element) {
    element.setAttribute('src', 'http://placeholderimage.com/100x100/ff00ff/fff');
}
function removeHoverEffect(element) {
    element.setAttribute('src', 'http://placeholderimage.com/100x100/000/fff');
}

Don't forget to include the following in your HTML:

<img src="my-src" onmouseover="addHoverEffect(this);" onmouseout="removeHoverEffect(this);" />

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

Learn how to display a "not found" message in a React.js application

I have a piece of code where I am sending a post request to an API and retrieving all the data from the API in a table. I am trying to find currency data based on the currency name, if found I display the data in a div, if not found I want to print "not ...

What is the correct way to utilize Html.Raw(Json.Encode(Model))?

I'm facing an issue with encoding my MVC Model using the code snippet below. The alert message is returning a null value, which is confusing to me since this is a create form. The HTML code structure is as follows: @using (Html.BeginForm()) { @H ...

Automatically navigate to a different page using JavaScript after 5 seconds without interrupting the execution of other code

Is there a way to redirect to a specific URL after 5 seconds without halting the execution of other code on the page? I want all the other code to run first before triggering the redirection. Wrapping the entire page in a setTimeout block is not an option. ...

Resize the div blocks by scaling the button placed beneath them

I decided to modify the g-recaptcha widget to fit my specific requirements, and it is working flawlessly. However, I noticed that the ng-click button directly beneath it is no longer responsive. The reason behind this issue eludes me. Here is a snippet of ...

Display Rails view using JavaScript when clicked

I have a task to create a view where users can click on different buttons and see different content displayed based on the button they choose. I attempted to achieve this using JavaScript, but unfortunately, I couldn't get it to work as intended. Init ...

Looking to display several charts on a single page with varying datasets?

I have successfully integrated a doughnut chart plugin into my website. However, I would like to display multiple charts on the same page with different data sets. Below is the code snippet for the current chart being used: This is the chart I am using & ...

Guide on triggering a function with the Enter key in Angular 9

A button on my Angular component currently triggers a method with a (click) event, but I also want the same method to be triggered if the user presses the enter key in the input box. This gives the user flexibility. Below is the HTML code for the input an ...

Which is better to use: sql==true or sql===true?

Hey there, could you please take a look at this and thanks in advance! I'm currently working on developing a system for upgrading buildings in my game. I have set up a universal upgrade div that applies to all buildings. When the player clicks on a bu ...

The icon for the weather on openweathermap is currently not displaying

Take a look at what my webpage looks like: http://prntscr.com/dg6dmm and also check out my codepen link: http://codepen.io/johnthorlby/pen/dOmaEr I am trying to extract the weather icon from the api call and display that icon (e.g. "02n") on the page base ...

"Implementing a Jquery event to handle the closing of a select

Is there a way for me to detect when the select tag is closed by clicking on the screen elsewhere? I can easily hook into the change event of the select fine, which will close the select. However, other methods such as body click, body focus, select blur, ...

Differences Between Vuetify Breakpoints and CSS Helper Classes

As I browse through the Vuetify documentation and various code snippets on the web, I often come across examples that mention using either a Vuetify breakpoint or a CSS helper class to make an element responsive to screen size changes. Is there a preferre ...

What steps should I take to ensure the height of this navigation bar is correct?

I'm attempting to ensure that this div displays the correct height (without manually specifying a fixed number like 75px). Here's how I want it to appear: ------------------------------------------ | something | something else | --- ...

Issue with static resource fetching when referencing a .js file within an HTML document while using Flask

My HTML file loads my OpenLayers JavaScript file (which displays a map frame) when opened directly. However, when running the HTML from my Flask python app, the JavaScript file/object fails to load (resulting in no map display, just some heading text). I ...

Why does the control skip the onreadystatechange function for the ajax object?

Just beginning my journey into web-development. Recently, I encountered an issue with a signup page I created that involves asynchronous calls to php. Upon debugging, I noticed that the onreadystatechange function is being completely skipped over. Any assi ...

Conflicting configurations in VS Code causing issues

Currently making adjustments to my JSON settings and everything is functioning correctly, however, I keep encountering this persistent error: "Expected comma jsonc(514)" Here's a snippet of my code that's causing the issue: "css.li ...

The jQuery AJAX post request is displaying an error message stating that the website xxx is not permitted by

I encountered a roadblock while attempting to use AJAX to call the eBay FindProducts API with a POST request. The error message that I got stuck on is as follows: XMLHttpRequest cannot load . Origin is not allowed by Access-Control-Allow-Origin. This ...

The Json request is invalid because it contains the @ symbol

After validating the Json data online, it appeared to be correct. However, when I attempted to call the json array key, the editor showed a Parsing error: Unexpected character '@' The editor in question is Visual Studio Code, and it seems as tho ...

Should I implement this practice when developing an AJAX website? Is it recommended to enable PHP code within .html files, or should I switch to using .php files instead?

Query: I am interested in executing PHP within HTML documents to include HTML using PHP include();. Question: Would it be more beneficial to change .php to .txt for my AJAX-loaded pages and switch my .html files to .php? This approach might resolve the ...

Iterating through HTML table data using a JQuery for each loop

Currently, I have an HTML table that is structured in the following way: <div class="timecard"> <h3>tommytest</h3> <table class="misc_items timecard_list" border="0" cellpadding="2" cellspacing="0" style="margin:0 auto;"> < ...

Tips for managing an event using the bxSlider callback API

I am currently using bxSlider to create a slideshow on my website. However, I now want to implement a manually controlled slideshow that also displays text content related to each image below the slideshow: Here is the code I have so far: <!--SlideSho ...