How can JQuery determine the current CSS background image of a three-state icon or button?

I am facing a challenge with using three images to represent different states of an application icon, and I am trying to achieve this using CSS.

There are no issues with the original or hover states:

#myDIV {
background-image: url(icons/homeBlack.png);
}

#myDIV:hover {
background-image: url(icons/homeWhite.png);
}

However, there are two additional things that I need to accomplish: 1) When the item is clicked, it should use the third image.

 $("#myDIV").click(function(event){

// change this button
$(this).css("background-image", "url(icons/homeBlue.png)");

});

2) If it is clicked again, it should not apply the third image again because it's already applied - so it needs to check if it has been applied.

My inquiries include: 1) Am I missing a CSS technique, or is jQuery the best solution for this? 2) Is there a way to determine which background-image is currently in place?

I have searched through jQuery but couldn't find anything that would help me identify the current background image.

Your assistance is greatly appreciated. Thank you.

Answer №1

Implement 3 unique CSS classes:

.style_1 {background-image: url(icons/homeBlack.png)}
.style_2 {background-image: url(icons/homeWhite.png)}
.style_3 {background-image: url(icons/homeBlue.png)}

When checking the state of the DIV, use the following approach:

     if ($('#myDIV').hasClass('style_3')) {......}
else if ($('#myDIV').hasClass('style_2')) {......}
else                                      {......}

To update the image, toggle between classes like so:

$('#myDIV').removeClass('style_1 style_3').addClass('style_2');

For hover events, utilize jQuery as CSS alone may not suffice, unless using ":hover", ":visited" selectors.

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

Tips on altering the quantity of columns in a ul list dynamically

I'm trying to create a list with 2 columns, and I want it to switch to 3 columns when the browser window is wide enough (for example, on a 23 inch monitor). Can this be achieved using CSS or any other method? Here is my current CSS: .search-results ...

The button event is currently only targeting the initial class. (Jquery)

I am having an issue where only the first instance of the saveBtn class is being saved into local storage when clicked. Can anyone provide some assistance with this? Here is the HTML: <div class="hour-container" id="8am"> & ...

iPad screen not displaying overflow for x and y axis

I'm encountering an issue with displaying the scrollbar on a div while using an iPad device. Currently, I am utilizing: -webkit-overflow-scrolling: touch; Although this is functioning properly, my goal is to have the scrollbar visible without the ...

Increasing Gap between Tabs in Primefaces AccordionPanel: A Guide

Is there a way to increase the spacing between the tabs of a Primefaces AccordionPanel component? ...

Utilizing checkboxes to toggle the visibility of a div element with varying headings

By toggling a checkbox, I aim to show or hide the same div with a different heading. $('#cbxShowHide').click(function() { this.checked ? $('#block').show(1000) : $('#block').hide(1000); }); #block { display: none; bac ...

Adjust the form input box height to a different size

Is there a way to adjust the height of the form input box so that the text is positioned close to the top and bottom borders of the box? I attempted to modify the CSS but was unsuccessful. Any suggestions for a Bootstrap CSS solution would be greatly appre ...

Is there a way to make divs expand on top of existing content when hovering over them, in order to avoid needing to scroll through overflow content? I am currently working with

I am working with 9 boxes contained within divs, each box includes data that exceeds the size of the box itself (represented by Some Text repeated for demonstration purposes). I am seeking a solution where hovering over any box will cause it to enlarge and ...

What is the best way to shorten a text using ellipses based on character count, rather than line breaks

If I have a text that needs to be trimmed down to 15 characters, like "I want to trim this text into 15 characters for example," the result should be something like "I want to limit..." in my email template. Is there a way to achieve this using just CSS? ...

How to Fix Items Being Pushed Down by 'Particleground' Jquery Plugin Due to Z-Index and Positioning

I'm grappling with understanding z-index and positioning, despite reading various questions and articles on the topic. Currently, I'm attempting to incorporate a Jquery Plugin called 'Particleground': https://github.com/jnicol/particle ...

Unseen components and columns with Bootstrap 4

I am attempting to hide a checkbox input in the middle of some column elements using Bootstrap 4. <div class="container"> <div class="row"> <a class="col-2"> 1 of 2 </a> <input type="checkbox" class="invisibl ...

What's the best approach when it comes to declaring CSS in an HTML document: should you

I have a quick question about HTML and CSS. Recently, I started using Twitter's Bootstrap framework and added this code snippet to the head of my index.html file: <link href="css/bootstrap.css" rel="stylesheet"> <link href="css/style.css" re ...

maintain ajax history during jquery redirection

Currently, I am designing a compact application using php, jquery, and ajax. The purpose of this app is to enable users to conduct customer searches, view customer details, and easily navigate back to the search page without losing any data. To enhance use ...

Unable to select the Icon's element

Hello everyone, I'm relatively new to using selenium and I've come across an icon element that I'm having trouble clicking. Below are the details of my issue: I attempted using this relative xpath, but I am still encountering a NoSuchElemen ...

The element will display above all other elements in its parent container

I am currently developing a React application, and the code structure is set up as follows: <Div> <Div style={{maxHeight:'60vh'}}> //This section includes the code for displaying a list item. Each item has its context menu that can ...

Tips for combining dvloading and required functionalities on the submit button

My form has 2 fields that must be filled out, and I used the required attribute to enforce this rule. Enter A:<input type="text" name="a" required><br> Enter B:<input type="text" name="b" required><br> <button type="submit" cl ...

Using Rails and ajax to dynamically choose where a partial should be rendered

In my html template, I have a loop statement generating multiple divs and buttons with unique ids. The resulting code resembles the following... // index.html.erb <button id="button-town-data1"><%= link_to 'Load Details', town_path(curr ...

Arranging Data in an HTML Table Using TableSorter

I have been trying to incorporate sortable columns into my HTML table and decided to experiment with the jquery tablesorter. I have followed the syntax below, but for some reason, my table is not allowing me to sort. Can you help me understand why? &l ...

Is there a way to ensure the collapsible item stays in its position?

I'm encountering an issue with the display of items within collapsible cards. Here is what it currently looks like: And this is how I want it to appear: Is there a way to achieve the desired layout using Bootstrap's Grid or Flex Layout? Here i ...

Issue with jQuery and AJAX on the webpage

I've recently implemented the search functionality using AJAX jQuery, but I am facing a problem. Currently, when I type something in the search field, the search list displays accordingly. However, upon refreshing the page, the search results disappea ...

Counting Numbers with jQuery Animation from Zero to Percentage Value

I am currently working on a project where I aim to incorporate a jQuery Animated Number Counter, starting from zero and ending at a specified percentage value. The values of the counter are dynamically retrieved from a database. function timer() { if ...