Create a CSS/HTML feature to disable image filters through a click event

On my website, I have a few images that serve as links. Normally, when I hover over one of these images with my mouse, it changes from grayscale to color; however, it reverts back to grayscale once the mouse is no longer hovering.

I want the image to remain in color even after clicking on it. If I click one image, it should stay colored until another link is clicked or activated.

Below is the code I am currently using:

HTML:

<p><a href="privileged.php" target="testframe">
<img class="grayscale" height="67" src="powerlogo.png" width="303">
</a></p>
<iframe name="testframe"></iframe>

CSS:

img.grayscale {
  filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\'/></filter></svg>#grayscale"); /* Firefox 3.5+ */
  filter: gray; /* IE6-9 */
  -webkit-filter: grayscale(100%); /* Chrome 19+ & Safari 6+ */
}

a:hover img, a:focus img {
    filter: none;
    -webkit-filter: grayscale(0%);
}

While this works correctly in Firefox, it does not have the desired effect in Chrome and IE.

What could be causing this issue?

Answer №1

One way to dynamically change the style of an element is by using JavaScript to add or toggle classes, and then styling those classes with CSS:

$("a").click(function(){
  $("a").removeClass("active");
  $(this).addClass("active");
});

a:hover img,
a:focus img,
a.active img {
  filter: none;
}

Check out this demo on jsFiddle!

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

React is unable to locate an import statement for Material UI components

I am facing an issue while incorporating material UI components into my React project. The error message I receive is related to an invalid import. Snippet from My Component File import React from 'react' import './middle.css' import Mi ...

Conceal a certain element in development using CSS

Is there a way to hide the footer section from my webpage without affecting the rest of the content? For example, using something like: "div class="poweredby" display: none", but I'm unsure of the correct method... Here is the spe ...

Issue: Stylesheet not being loaded on Index.html through Gulp

Issue The .css file is not being loaded by the index.html despite setting up a gulp.js file. It seems like the folder directory might be causing the problem. However, the .scss files in the /src/scss folder are compiling correctly into CSS within the /bui ...

Refine the inventory by using data-price attributes to narrow down the product selection

While I have successfully implemented the filtering of the product list based on store and brand in my code, I am facing challenges in filtering it with price range. <div id="prod"> <div class="content" data-brand="Andrew" data-price="1000" d ...

How can I configure the domain in a Localstorage offline JS app to access local storage from multiple HTML files?

For a small JS/HTML app I am developing, I need to store preferences in LocalStorage and access them from two different HTML files. While it works fine when staying on the same HTML file, accessing the stored data from another HTML page poses a challenge. ...

Trigger the opening of a class or window upon pressing the button

Good evening, I'm attempting to create a functionality where pressing a specific button will open a window. However, I am unsure of how to achieve this using CSS classes. My initial thought was to add a new class in the CSS file and call it every ti ...

Seeking assistance with the manipulation of divs based on the width of the screen for hiding

Recently, I encountered an issue with changing a 3x3 image gallery layout to an image slider when the screen size was less than 768px (mobile). Thanks to the help of everyone and particularly Danilo, who provided solutions in the comments, I was able to id ...

Odd Behavior When Using jQuery for Smooth Scrolling to a Specific Div

I am currently working on a website with a fixed header and three main sections stacked one after the other. There is a link in the header that allows for smooth scrolling to the last section. However, I am facing an issue where the scrolling behavior togg ...

Issues with the Lengthmenu option in Bootstrap Datatables Javascript

I'm currently facing an issue with my bootstrap datatables searchable table. Everything is working fine, except for the fact that users are unable to choose how many rows they want to see per page. The default setting of 10 results per page is not suf ...

resembling the nth-child selection for even elements within a list wrapped in <ul> tags

I recently utilized ng-repeat to iterate over a ul element. Unfortunately, I've run into an issue where I can't simply use nth-child(even) to change the background color of every other ul element. Does anyone have any alternative solutions or s ...

Issue with Google Maps: undesired change in map size

Welcome to my app! Check out the link to my app here: http://jsbin.com/axeWOwAN/1/edit If you prefer a full-screen view, click here: http://jsbin.com/axeWOwAN/1 I'm encountering an issue with the map on the second page of my app. The map works perf ...

Right and left floating elements align side by side

The alignment of the images and file labels needs adjustment to ensure they all appear on the same line. Below is the code snippet that I have tried: HTML: <div> <img src="{{asset('bundles/cramifaccueil/images/pdfdocument.png')} ...

"Transforming a div's data-src to an image_tag in Django - A Step-by-

I am struggling to display images in my project using static tags. I have tried various methods but none seem to work with the div tag. After unsuccessful attempts, I decided to seek help online but couldn't find a solution. Any assistance would be g ...

displaying data in a table - adjust cell contents for smaller screens

I am currently designing an admin page using Bootstrap 5. I have implemented a data table with the following contents: On larger screen sizes: https://i.sstatic.net/2a9C1.png For smaller screen sizes (mobile phones), I have collapsed the action button as ...

analyzing information from an online source

I'm currently developing a program that displays the maximum daily temperature for a specific city by utilizing data retrieved from the National Weather Service website. I have all the necessary data, however, whenever I attempt to create the actual g ...

What is the most effective method for creating a dark background with light text in Bootstrap without using SASS?

I recently created a new website using Bootstrap 5 and set the background color to black (#000000) by adding some CSS styling. However, I noticed that this change had unintended consequences on the appearance of various elements such as drop shadows, modal ...

Incorporate a TextBox into your ASP.Net Menu

Currently, here is the code I am working with: <div class="clear hideSkiplink"> <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal"> ...

Looping through jQuery click() function

I used a loop to generate div elements and I am trying to modify the background-color when the div is clicked. var c = $("#container")[0]; for(var i=0; i<6; i++){ var x = document.createElement("div"); x.className = "sqare"; x.click(changecolor(t ...

Ways to update text randomly without the need to refresh the page

Currently, I have a script that provides users with a random word. However, the only way to receive a new word is by refreshing the page... I would like to be able to set the words to change at specific intervals rather than needing to reload the page or ...

Use Javascript or Jquery to dynamically change the background color of cells in HTML tables based on their numerical

I am working with a collection of HTML tables that contain numbers presented in a specific style: <table border="1"> <tr> <th>Day</th> <th>Time</th> <th>A</th> <th>B</th> &l ...