Switch out image with Jquery when hovering, and revert to default on mouse out

Despite the numerous times this question has been asked, I am struggling to find a solution that fits my specific case. Let's dive into it.

My navigation setup involves using Bootstrap nav pills to navigate through content. The structure looks like this:

<ul id="mytabs" class="nav nav-pills nav-justified" role="tablist">
  <li class="option1"><a href="#option1" role="tab" data-toggle="tab"><img src="/image1" id="image1-background"</a></li>
  <li class="option2"><a href="#option2" role="tab" data-toggle="tab"><img src="/image2" id="image2-background"</a></li>
</ul>

<div class="tab-content">
    <div class="tab-pane fade id="option1">
        <p>Content for first option</p>
    </div>
    <div class="tab-pane fade id="option2">
        <p>Content for second option</p>
    </div>
</div>

Although seemingly simple, let me explain further. The key concept behind the .nav-pills section is that they contain images that should change on hover and mouse over events. I accomplish this with the following script (which gets me close to what I need):

$(".option1").hover(function() {
    $('#image1-background').attr('src', 'different-image1.png');
}, function() {
    $('#image1-background').attr('src', 'image1.png');
});

While this works as intended by changing the images correctly, the issue arises when I want the different-image1.png to remain displayed when hovering over the

.tab-content > .tab-pane id="option1"
section. However, this doesn't work because the image changes upon hover. How can I keep the hovered image ('different-image1.png' in this scenario) displayed when my cursor is in the corresponding tab?

I attempted the following solution:

$('#mytabs a[href="#option1"]').on('shown.bs.tab', function (e) {
        $('#image1-background').attr('src', 'different-image1.png');
});

However, this approach didn't yield the desired results unlike other scripts provided by Bootstrap. For a clearer understanding of the issue, here is a jsfiddle link - https://jsfiddle.net/adaccount/9rdum27v/

Answer №1

To improve the functionality, create a wrapper div to contain both tabs and tab content. Attach a mouse enter event to the tab options only, and set up a mouse leave event for the wrapper to reset to its default state.

An updated version of your example can be found here:

https://jsfiddle.net/9rdum27v/1/

d

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

Postman: Iterating through requests with various input data sets to dynamically generate the request body

I am facing a challenge with my login API request that requires 3 parameters (userName, password, and remember) in the request body. Out of these parameters, userName and password are mandatory, while remember is optional. The input data is being pulled fr ...

"Using the push method in JavaScript allows for the combination of arrays rather than

Looking to retrieve data from an API and store it in an array. When I assign the value directly using '=', the desired data is displayed. However, when attempting to add elements using 'push', they are added as another array. Below is ...

Govern Your Gateway with Expressive Logs

I'm facing some issues with the logs in my Express Gateway: Although I followed the documentation and enabled Express Gateway logs, I cannot locate any log files under my gateway root directory. Whenever I start the gateway using the command LOG_L ...

NextJs not processing Bootstrap form submissions

I’m struggling to figure out why my form isn’t submitting when I click the submit button. The backend seems fine because Postman successfully sends the information to the database. However, nothing happens when I try to submit the form. My tech stack ...

Ways to ensure that the ul navigation bar spans the entire bottom of the page evenly (without resorting to using table

This question has been previously raised on Stack Overflow with an insightful answer provided. However, I am encountering a specific problem in implementing it. My objective is to have a navigation bar (nav consisting of ul and its li items) occupy the en ...

Developing a two-dimensional JavaScript array using an AJAX PHP request

I've been working with a MySQL table that stores image data. My goal is to extract this image data and store it in a JavaScript array. The fields I need for the array are "image_ref" and "image_name." To achieve this, I understand that I'll nee ...

Is it possible to update the page title with JQuery or JavaScript?

Is it possible to modify the page title using a tag inside the body of the document with jQuery or JavaScript? ...

Performing a conditional query on a Many-to-Many relationship in TypeORM

Operating under a many-to-many relationship between Category and Product entities In need of filtering products based on category ID Attempted to implement the following code after referring to some examples, but encountered difficulties in making it fun ...

Moving between different perspectives within a single-page backbone application

I am interested in incorporating dynamic transitions similar to the ones showcased on this website into a single-page backbone application. However, I am unsure of where to begin. Do I need to modify how I initialize my views (currently using a standard sw ...

limited growth of float variable

I am utilizing CSS code to create column a and column b, using float in the code to dynamically expand column a as content is added to column b. However, this expansion is not occurring as expected. To see my code, please visit http://jsfiddle.net/hadinetc ...

Troubleshooting CSS Problems: Issues with background repeating and footer placement

I am currently in the process of transitioning my website to a CSS layout, and so far it's looking good. However, I am facing two specific issues with the page layout that I need help with: 1) The background image of the left-most column is not repea ...

Error: Node requires the use of 'import' to load ES modules

Here is the code snippet that I am currently working with: import { toString } from 'nlcst-to-string'; import { retext } from 'retext'; import retextPos from 'retext-pos'; import retextKeywords from 'retext-keywords' ...

Can someone please provide a reference to an online illustration of implementing TinyMCE with a print format similar to Microsoft Word

Are there any online resources demonstrating the use of TinyMCE with a print layout resembling Word, with visible page breaks? Alternatively, are there any other WYSIWYG editors with a built-in print layout feature available for open source use? ...

Issue: Upon attempting to connect to a vsftpd server deployed on AWS using the npm module ssh2-sftp-client, all designated authentication methods have failed

Code snippet for connecting to the vsftpd server sftp.connect({ host: "3.6.75.65" port: "22" username: "ashish-ftp" password: "*******" }) .then(() => { console.log("result") }) .catch((err)=>{ ...

iPhone's color selection tool for choosing hues

After implementing the code for a color picker, I noticed that in Safari browsers on iPhone, the input is displayed as a textbox showing the color hex value without a color palette. Is there an alternative solution that would work across all mobile brows ...

The body element is not positioned at the top of the webpage in the HTML/CSS layout

I'm running into an issue with my HTML/CSS. It seems like my body tag has unexpectedly acquired a margin-top of 10px... However, I've searched high and low but can't seem to figure out how to remove it. This is something that has never happe ...

Issues with toggling the menu on Angular 14 using Bootstrap 4.6

I am struggling with implementing a menu on the header section of my Angular 14 homepage. The menu I added is not opening as expected. Even after trying various working menu samples from the internet, I couldn't get it to work in my project. Here are ...

What steps can be taken to remove a server-side validation error message once the user has inputted the correct value?

I'm facing an issue with server-side validation messages on my form, which includes fields for Name, Mobile, Email, and Message. While JQuery validation works fine, clearing the error message after user input is causing a problem. Using AJAX to submi ...

Is it possible to execute "green arrow" unit tests directly with Mocha in IntelliJ IDEA, even when Karma and Mocha are both installed?

My unit tests are set up using Karma and Mocha. The reason I use Karma is because some of the functionality being tested requires a web browser, even if it's just a fake headless one. However, most of my code can be run in either a browser or Node.js. ...

Is there a way to integrate a MySQL database with parcel-bundler in a Node.js environment, or is there a simpler method to achieve this database integration with parcel-bundler?

Node.js and parcel-bundler are new to me, but I've managed to create a server.js file that connects to the database without any issues. Server.js const express = require('express'); const mysql = require('mysql'); //Establish con ...