CSS/HTML: Remove image filter on click

11,915

You can add/toggle a class with javascript to your element and style this with css:

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

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

http://fiddle.jshell.net/3uYDc/

Share:
11,915
dnn
Author by

dnn

Updated on June 04, 2022

Comments

  • dnn
    dnn almost 2 years

    I use some images as links on my site. When I hover the mouse over one of the images, they go from gray to color - and they go back to gray when the mouse is no longer hovering.

    I want the image to stay colorized when activated. Say I click the image, it should stay colorized until another link is clicked/activated.

    This is my code:

    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%);
    }
    

    The above works as intended in Firefox, but not in Chrome and IE.

    What am I doing wrong?

  • dnn
    dnn almost 11 years
    Thanks alot, this worked perfectly. But please take it easy, I was at work so I haven't been able to answer before now.