Restoring hover functionality after resetting color using jQuery - tips and tricks

My jQuery code is working well, but I am facing one issue. After clicking on my infocontent menu, the hover effect on h1 is no longer working. How can I bring back the hover function?

Here is the HTML:

<div class="infocontent"><h1>What?</h1></div>
<div class="infocontent"><h1>From/ For whom?</h1></div>
<div class="infocontent"><h1>Actions</h1></div>
<div class="infocontent"><h1>With whom?</h1></div>

CSS style used:

.infocontent {
    width: 120px;
    height: 120;
    float: left;
}

.infocontent h1 {
    width: 100%;
    font-size: 24px;
    text-transform: uppercase;
    color: #999;
}

.infocontent h1:hover { color: #000; }

The jQuery script being used:

$('.infocontent h1:first').css('color', '#000');

$(".infocontent h1").on("click", function() {
    $('.infocontent h1').css('color', '#999');
    $(this).css('color', '#000');
});

View Demo Here

Answer №1

To simplify things, you could modify the JavaScript to switch classes using the following code:

$('.infocontent h1').eq(0).addClass('on').end().on("click", function() {
    if(!$(this).hasClass('on')) {
        $('.on').removeClass('on'); 
        $(this).addClass('on');
    }
});

You can also update the CSS with these changes:

UPDATE: I noticed you wanted background color changes

.infocontent{float: left}

.infocontent h1 {
    text-transform: uppercase;
    color: #999999;
    /* additional modifications */
    font-size: 18px;
    padding:3px 8px;
    letter-spacing:2px;
    cursor:pointer/*to indicate it is clickable*/  
}

.infocontent h1.on,.infocontent h1.on:hover{color:#000000;background-color:#CCCCCC}
.infocontent h1:hover {color:#666666;background-color:#EDEDED}

I have made the necessary adjustments in your fiddle (now includes background color changes): http://jsfiddle.net/filever10/FXetb/18/

Answer №2

To override the inline CSS added by jQuery, you must use !important unfortunately.

New CSS Guidelines

.infocontent h1:hover {
    color: #000!important;
}

jsFiddle - see it in action.

I recommend avoiding the excessive use of !important. Instead, consider modifying the jQuery to apply or remove a CSS class instead of directly adding inline styles.

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

In my Cordova application, I am able to print the locally stored JSON array, but I am encountering an issue where the

Hello, I'm having some difficulties with JSON as a beginner. I've tried searching extensively but haven't found a solution that works for me. My issue arises when attempting to save data using local storage in JSON format - the array prints ...

How can you receive a file through AJAX response?

I have a standard Ajax function that I regularly use to call a specific function within my node.js server. This particular function is responsible for retrieving a file (usually in xls format) from the server and streaming it back to the client. Below is ...

Is it possible to determine if a window was opened using window.showModalDialog()?

Is there a way to determine if a window was triggered by window.showModalDialog()? I've noticed that when a new window is opened using window.open(), window.opener correctly returns the parent window. However, when using window.showModalDialog(), wind ...

angularjs bootstrap carousel not functioning as expected

HTML How do I apply an active class to the first image? I am trying to create a slideshow with images from a database but encountering issues. <div id="myCarousel" class="carousel slide" data-ride="carousel"> <div ...

Persistent vertical menu dropdown that remains expanded on sub menu pages

I am struggling to understand how to keep my menu sub items open when on the active page. Although I have tried similar solutions, I have not been successful in implementing them. I apologize if this question has been asked before. My approach involves usi ...

Designing a dynamic button that expands on hover to catch the eye

My instructor has provided us with this code snippet: <a class="btn btn-tertiary" href="#"> <span class="circle"> <i class="arrow-right"> </i> </span>Create profile </a> .btn.bt ...

Guide to aligning form data with Bootstrap

Struggling to align my form data in the center below. I have attempted various online solutions without success. The use of bootstrap and offset is causing issues with the title for the form and the actual form itself. Any suggestions would be highly appre ...

How to use jQuery to dynamically generate a JSON object within a loop

http://jsfiddle.net/hU89p/219/ This example demonstrates the creation of a JSON object based on selected checkboxes. When selecting one checkbox, the JSON would look like: var data={"name":"BlackBerry Bold 9650", "rating":"2/5", "Location":UK}; By sele ...

Does CSS give preference to max width specifications?

Within a div, I have two child div elements. The parent container utilizes a flexbox layout, with preset maximum widths for the children as shown below: <div class="wrap"> <div class="one">Hi</div> <div class="two">my secon ...

Adjusting the appearance of internal components with material-ui icons

Currently working on a project using react and material-ui where I am looking to customize the styles of specific elements within components. An example is the ListItem component which includes primaryText, leftIcon among others, and my goal is to modify t ...

Crafting a personalized arrow for sorting headers in Angular Material

Currently working on an Angular 5 project and I'm looking to implement a custom sort icon in the header. The goal is to achieve a similar effect to this example, without using the default arrow. I attempted to modify the CSS styles, but it wasn' ...

Using jQuery to replace the content of a div with a delay?

I am attempting to utilize jQuery to create a fade effect on an element, replace its innerHTML content, and then fade it back in once the new content has been added. I have successfully managed to replace the element's content using the .html() method ...

Is it possible to customize the deep elements of ExpansionPanelSummary using styled-components in React?

After digging into the documentation and examples on how to customize Material UI styling with styled-components, I successfully applied styling to the root and "deeper elements" within an ExpansionPanel and ExpansionPanelDetails. However, when attempting ...

Is there a way to combine fixed and dynamic size elements within a flexbox container?

Currently, I am working on creating a layout that combines the automatic sizing feature of flexbox with fixed-size elements: <View style={{ height: 70, alignItems: "center" }}> <Text style={{ flex: 1, textAlign: "right", paddingRight: 10 }}&g ...

What is the process for adding a download link to my canvas once I have updated the image?

Is it possible to create a download link for an image that rotates when clicked, and then allows the user to download the updated image? Any help would be appreciated.////////////////////////////////////////////////////////////////////// <!DOCTYPE ht ...

Is it possible for a "position:absolute" div to maintain its relative width?

Imagine having two nested divs like the following: <html> <body> <div id="outer" style="width:50%"> <div id="inner" style="width:100%"> </div> </div> </body> </html> Currently, the i ...

Arrange multiple divs on top of each other by utilizing the Bootstrap flexbox system

Apologies for my lack of experience with Bootstrap, specifically version 4.0. I have been researching extensively but am struggling to accomplish the simple task of stacking two divs on top of each other. <div class="d-flex flex-column"> <div ...

Select multiple options by checking checkboxes in each row using React

Is it possible to display multiple select checkboxes with more than one per row? For example, I have four options [a, b, c, d]. How can I arrange it to have 2 options per row, totaling 2 rows instead of having 1 option per row for 4 rows? ☑a ☑b ☑c ...

"Despite receiving a response code of 200 in Rails using jQuery ajax, the data is still

I'm attempting to display a partial in a bootstrap modal. Although I am receiving a 200 response, the JavaScript code within show.haml is not functioning correctly. I have added an alert message for testing purposes, but it does not trigger. In the GE ...

Error: Unable to retrieve the value as the property is null

I'm a beginner in jQuery and I'm attempting to create a login form that displays a message when the user enters a short username. However, despite my efforts, the button does not trigger any action when clicked. Upon checking the console, it indi ...