0

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:

https://jsfiddle.net/md2100/fwcdzhu2/

Here's what my current code looks like.

HTML:

<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>

Javascript:

$(document).ready(function(){
$("button").click(function(){
$("p").css("filter", "invert(100%)");
});
});
2
  • I'm not seeing the filter property in your code. Commented May 5, 2016 at 22:28
  • idem, jsfiddle.net/fwcdzhu2/3 but doesnt apply only to bg Commented May 5, 2016 at 22:29

3 Answers 3

1

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

Demo: https://jsfiddle.net/fwcdzhu2/2/

Sign up to request clarification or add additional context in comments.

Comments

1

Webkit announced they are dropping prefixing. Because of this, I would consider cleaning things up a bit. If you need to remove the -webkit-filter at some point, removing it from CSS is way less annoying than doing it from JavaScript.

CSS

.my-filter {
  -webkit-filter: invert(100%);
  filter: invert(100%);
}

JS

$("p").addClass("my-filter");

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.