Dynamically apply CSS filter

12,096

Solution 1

You should set value to the webkitFilter property, not to the style object. This syntax will work:

image.style.webkitFilter = "brightness(50%)";

If you don't know JavaScript property name, you can reference it by CSS property (like karaxuna suggested, will work too):

image.style["-webkit-filter"] = "brightness(50%)";

Solution 2

image.style["-webkit-filter"] = "brightness(50%)";

Solution 3

Add that filter to a class:

.bright {
    -webkit-filter: brightness(50%);
}

And toggle that class:

image.classList.toggle('bright');
Share:
12,096
Jacob
Author by

Jacob

hunt to live live to hunt

Updated on June 08, 2022

Comments

  • Jacob
    Jacob almost 2 years

    How to dynamically apply CSS filters? I have tried the following for Chrome.

    image.style = "-webkit-filter: brightness(50%);";
    
  • Blender
    Blender about 11 years
    @PavloMykhalov: Well, neither are -webkit properties.
  • Maciej Jankowski
    Maciej Jankowski over 9 years
    any idea how to combine two filters? say brightness and invert?
  • Pavlo
    Pavlo over 9 years
    @MaciejJankowski just separate them with a space, "brightness(50%) invert()": jsfiddle.net/bj4p0sxm