Engage in a sequence of actions by tapping and retapping on a div element

Is there a way to toggle the color of an element from black to orange with a single click and back again with another click? Additionally, I have set up a hover effect in CSS that changes the color to orange when hovered over.

In my current code, the element successfully transitions to orange when hovered over and changes color on click. However, after clicking twice, the hover effect stops working and the element remains black even when hovered over.

This is my HTML setup:

<div id="activitys">
   <div class="pictures_activitys"></div>
</div>

Here is my JavaScript implementation:

$( document ).ready(function() {
    var jogging_selected = false;

    $( ".pictures_activitys:nth-child(1)" ).click(function () {

        if(jogging_selected){
            $(".pictures_activitys:nth-child(1)").css( "background", "url(img/sports/jogging_black.png)" );
        }
        else{
            $(".pictures_activitys:nth-child(1)").css( "background", "url(img/sports/jogging_orange.png)" );

        }
        jogging_selected = !jogging_selected;

    });
});

And this is how I've styled it in CSS:

.pictures_activitys{
    margin: 30px;
    margin-top: 80px;
    width: 128px;
    height: 128px;
}
.pictures_activitys:nth-child(1){
    background:url(../img/sports/jogging_black.png);
}
.pictures_activitys:nth-child(1):hover{
    background:url(../img/sports/jogging_orange.png);
    cursor: pointer;
}

Does anyone know why the hover effect stops working after double-clicking on the element?

Answer №1

Try including !important in your background property when hovering

.images_tasks:nth-child(1):hover{
    background:url(../img/sports/running_orange.png) !important;
    cursor: pointer;
}

This is necessary because the script has added inline styling to the element, causing it to override your CSS. Using !important will ensure that the element uses the background defined in the CSS and overrides the inline style

https://jsfiddle.net/SampleUser/abcd1234/

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

Ways to perfectly align three images side by side using CSS

I am trying to align 3 pictures horizontally, but they keep ending up on separate lines. Here is the code I have been using: HTML <section class="features"> <figure> <img src="....jpg" alt="..."> ...

Occupying both horizontal and vertical space in a Bootstrap column

I have set up my buttons like this: https://i.stack.imgur.com/OMfhp.png In the image, you can see that the Left Option Button is larger, and I want all buttons to be like that. They are supposed to be aligned next to each other as I am using the Bootstra ...

Safari showing white flash upon setting background-image src using Intersection Observer?

I've done extensive research but I'm still struggling to solve this issue. It only seems to be happening on Safari (Mac & iOS), while everything works smoothly on Chrome, Firefox, Edge, and other browsers. UPDATE: The flickering problem also per ...

I am attempting to fetch the posts using an ajax call, however, I am encountering a server error 500 with Wordpress Ajax

After clicking on a pagination link (page number), the post_type, taxonomy, and term_name are retrieved. Using JQuery Variables, these variables are passed to an Ajax WordPress function. Although the Ajax function successfully receives the variables, the W ...

Obtain the values from controls within the <td> element

I'm experiencing some difficulty creating an array of table cell values because the cells contain controls, not just plain text. $("#tbl_CGT tr").each(function() { var arrayOfThisRow = []; var tableData = $(this).find('td'); if (tab ...

Is it possible to transmit an image using ajax and php without using a form or input type file?

Seeking a unique solution here. I am interested in enabling a straightforward image upload directly from a mobile camera. No need for a form, just display the image in a preview and allow the user to click to upload it to a PHP script for processing. I&ap ...

Django view returns incorrect HTML template after receiving AJAX GET request from jQuery

In my web application built with Django, the index.html page utilizes jquery fullcalendar to display events. These events are populated by a Django view called index() which uses simplejson to dump an array containing all events for the current month. The ...

Pass a URL string to a different script using Node.js

Currently, I am delving into the world of Node.js, utilizing Express with Jade and Mongoose as my primary tools. Originating from a background in PHP, transitioning to Python, and embracing MVC principles through Django, my aspiration is to create a multip ...

Updates to Providers in the latest release of Angular 2

When working with angular 2.0.0-rc.1, we implemented a Provider using the new Provider method as shown in the code snippet below: var constAccessor = new Provider(NG_VALUE_ACCESSOR, { useExisting: forwardRef(() => EJDefaultValueAccessor), ...

Latest updates on the JSON and PHP news platform

Currently, I have the ability to access an API that produces news in JSON format. As an illustration, by providing the news ID #9, I can fetch the data from: someserver.com/core/news/9 Upon visiting this page, it yields the required JSON data for the new ...

Sending Ajax requests to web services hosted on a dual-node NLB

I am currently managing a two-node NLB configuration where multiple web services need to be accessed from the client-side using ajax POST requests. When visiting the page at: http://clusternode1/ everything works smoothly. Similarly, when accessing it f ...

JavaScript running out of memory during yarn build process

While working with yarn to build my project, I encountered a problem. The issue at hand is that the memory heap exceeds its allocated size, causing the build process to fail. FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - Java ...

Navigating through the Express Routing: Homepage and Error 404

I've been working on a Node application with Express, and currently have the following code snippet in my app: app.use('/', function(req, res) { res.render('index', {}); }); While this route is functioning properly ...

Tips for displaying dynamic images using the combination of the base URL and file name in an *ngFor loop

I have a base URL which is http://www.example.com, and the file names are coming from an API stored in the dataSource array as shown below: [ { "bid": "2", "bnam": "ChickenChilli", "adds": "nsnnsnw, nnsnsnsn", "pdap": " ...

Generate an adjustable grid layout (with rows and columns) to display information retrieved from an API request

I have recently started learning React JS and JavaScript. To practice, I am working on a small project where I am facing an issue with creating dynamic rows and columns. My aim is to display data in 4 columns initially and then move to a new row and column ...

Encountering undefined JavaScript functions when called from HTML

I had most of these functions working perfectly, but after restarting my program, I'm now encountering issues with undefined functions in Chrome. I can't pinpoint the exact problem, and even though I've checked through Eclipse, I still can&a ...

How to Embed a Javascript (jquery) Variable within a JSON Object

I have searched everywhere for a "simple answer" to this problem, but unfortunately, I cannot find a solution that aligns with my understanding of JSON and jQuery. Perhaps I am too much of a beginner in JSON to accurately formulate the right question (and ...

When double quotes are used in a string within an HTML input field, they may display as &

Using PHP, I am retrieving data from an SQL database and generating JavaScript code to create an edit button for each entry. The edit button triggers a text input field within a form. When the user clicks on the generated edit button, the text in this inpu ...

Three brothers and sisters - One must expand responsively while the remaining two maintain minimum content sizes

Attempting to outline the limitations at hand. We have 3 containers named .content, .left, and .bottom, along with a resize handler that governs the available space. I aim for the .container to expand as the space increases, utilizing all available area. ...

Does anyone have a solution for resetting the ID numbers in a flatlist after removing an item from it?

As the title suggests, I am facing an issue with the following scenario: { id: '1', name: 'one' }, { id: '2', name: 'two' }, { id: '3', name: 'three' }, { id: '4', name: &apo ...