How can I use Jquery to switch images when hovering?

I'm having trouble getting an arrow icon on my image slider to change when I hover over the image. I've tried different jquery methods, but nothing seems to be working. All I want is for the image to change without any fancy effects like fade-outs.

Here's the HTML:

<div class="next"><img src="next.png"></div>
<div class="prev"><img src="prev.png"></div>

And here's the CSS:

.next img{
position: absolute;
height: 50px;
width: 50px;
top: 315px;
right: 570px;
z-index: 99;
}

.prev img{
position: absolute;
height: 50px;
width: 50px;
top: 315px;
left: 550px;
z-index: 99;

}

Lastly, the Jquery code:

$('.next').hover(function(){
$(this).attr('src','prev.png');
},function(){
 $(this).attr('src', 'next.png'); 
});

Answer №1

This is an example of achieving hover effects without using JavaScript:

<div class="next"></div>
<div class="prev"></div>

.next {
  background: url(next.png) no-repeat;
}
.next:hover {
  background: url(prev.png) no-repeat;
}

.prev {
  background: url(prev.png) no-repeat;
}
.prev:hover {
  background: url(next.png) no-repeat;
}

Answer №2

next refers to the div element, not the img. Therefore, you will need to locate the img inside the next element and update its src attribute.

$('.next').hover(function () {
    $(this).find('img').attr('src', 'prev.png');
}, function () {
    $(this).find('img').attr('src', 'next.png');
});

See it in action: Demo

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

``Incorporating best practices: Setting the height of the parent container equal to the height of

When designing a container, is it considered beneficial to assign each child within the container its own height property, even if the parent container has already been allocated a specific height? If not, are there alternate methods available for transfer ...

What is the process for making an ajax call in CakePHP?

It may seem obvious, but I've searched the internet and couldn't find any useful or updated information on this topic. I'm attempting to make an ajax request in CakePHP controller to retrieve both view contents and JavaScript code. The aja ...

Creating a Personalized Tab Layout with react-bootstrap

Incorporating react-bootstrap components and styled components, I have implemented tabs in my project. The current appearance of the tabs is as shown here: https://i.sstatic.net/rPnlO.png However, my requirement is to have the tabs look like this inst ...

A guide on using JavaScript to obtain the original calculation for a specific equation once a checkbox has been unchecked

I am facing a scenario where I have two input fields. One should display the value of Quantity, and the other should display the value of Price. The first input, which is for quantity and of type number, has the disabled attribute. However, upon checking a ...

There are two borders in place, with the second border present only on the right and bottom

Can someone help me achieve borders like the ones in this image? https://i.sstatic.net/qZHbK.png I tried using the after pseudo-element as shown below, but after learning from this discussion: Why can't an element with a z-index value cover its child ...

What is the process of invoking a function in ajax from a web service that includes a PDF attachment?

I know how to download a file in my web services, but I'm unsure of how to call this function using ajax. Within my datatable, there is a button column. When I click on a button within the table, I want to be able to download a PDF file from the SQL ...

Assistance in using jQuery to locate specific div elements is

I am currently working on creating a navigation bar that features icons triggering contextual submenus upon hover. The main idea is that hovering over an icon will display a popup menu or tooltip with additional options, while still allowing the icon itsel ...

Is it feasible to retrieve a JavaScript code housed in a script file from an iframe?

Seeking to access a JavaScript function from a script file on another page using an iframe. Here is a snippet of my code: The page where the JavaScript needs to be accessed. <iframe id="FRAMESET" src="default.htm" width="0%" height="0%"> & ...

What steps can be taken to resolve the following error message: "No route found for 'GET /etat/delete_emp_nbre_administrative/101': Method Not Allowed (Allow: DELETE)?"

I have implemented a new function in my controller using a route annotation with the method set to "DELETE". However, when I try to create a form in Twig that includes a delete button, I encounter the following error: No route found for "GET /etat/delet ...

How can I find the last element that was selected using XPath in the browser console

Need help with XPath: $x(("//div[@class='ag-header-row']))[1] I'm working with an array of divs, but I want to select the last one. The [1] is necessary due to multiple rows with this class. I’ve heard about using [last()], but unsure w ...

What is the best way to style the header of a table when scrolling in CSS?

Currently, I am facing an issue with applying the top CSS property to the thead element of a table while scrolling. I have attempted various methods but have been unsuccessful in achieving the desired outcome. Initially, I used the scroll event, however, ...

Designing a split navigation bar in Django using unique styling for each section (HTML, CSS, and Javascript)

In my endeavor to design a search button within a responsive navigation bar, I encountered an issue. The navigation bar contains a media query in the CSS file that adjusts it to a dropdown list based on the reader's screen dimensions. Here is the HTM ...

Adding a class name to the three active, visible, and shown slides in SwiperJS is a simple process that can be done by

Currently, I am working on customizing a carousel in Swiper with slidesPerView={3}. My goal is to style the sliders that are not visible on the screen. I have made multiple attempts to achieve this: First, I tried adding the attribute slideActiveClass="sw ...

Displaying a Div Element using jQuery based on a PHP list

After researching how to toggle a div using jQuery based on dropdown selection made with "select" and options, I am facing a challenge. The dropdown selection code I am working with is as follows: <?php echo $this->lists['catid']; ?> T ...

Connection between overlay div and corresponding destination div address

I created a linked image with an overlay div: <div class="imageBlock"> <a href="http://www.google.com"> <img src="https://placeimg.com/640/480/any"> </a> <a href="http://www.twitter.com"> <img src="https://pl ...

Issue with Bootstrap table not identifying rows created with AJAX calls

Just to provide some context: I am currently utilizing Bootstrap-table with Bootstrap version 3.3.6 My objective is to populate a table using AJAX and jQuery, but I'm encountering an issue where none of the bootstrap extensions seem to function proper ...

django reverse url sending requests to itself

In my project, I have a base URL file that includes all the app's URL files. What I am trying to achieve is that upon clicking the Generate PDF Button, I want to call an APIView/view-function passing a variable as a parameter using the get method with ...

Retrieve the chosen item to automatically fill in the input fields using Ionic 2 and Angular

My goal is to create a dropdown menu populated with a list of items, and when a product is selected, its price should automatically appear in the quantity field. <ion-item> <ion-label>Item</ion-label> <ion-select (ionChange)="getP ...

Limiting the types of files a user can access through a web browser

Hey there! I have a unique question that sets it apart from others, as it involves restricting file types for users with a browser popup. My goal is to prevent users from selecting certain file types before they choose a file. I've come across some s ...

What is the best way to send files and additional data through AJAX in a POST request to a PHP page and securely store it in a database?

I have a form that contains text data, and I want to use the ajax POST method to send the form content and file to a Proccess.php handler. My goal is to receive the binary file as an object and insert it into a MySQL database. I understand that storing fil ...