dougblitz210 dougblitz210 - 11 months ago 35
Javascript Question

Using the CSS "filter" property with jQueries css method

It seems I cannot apply the css "filter" property with the .css method in jQuery. Other properties seem to work okay when they are written this way, but not the filter property... jsFiddle here:

Here's what my current code looks like.


<h2>This is a heading</h2>
<p style="background-color:#ff0000">This is a paragraph.</p>
<p style="background-color:#00ff00">This is a paragraph.</p>
<p style="background-color:#0000ff">This is a paragraph.</p>
<p>This is a paragraph.</p>
<button>Set background-color of p</button>


$("p").css("filter", "invert(100%)");


You need to set the filter attribute if you are trying to set the filter via .css() - Also, you need to include the browser prefixing to make it work in chrome, opera, and safari.

Keep in mind that filter does not have full cross-browser support!

$("p").css({"-webkit-filter": "invert(100%)", "filter": "invert(100%)"});