mildog8
mildog8

Reputation: 2154

jQuery inline style rgb fallback for rgba color IE 8

I am using jQuery to set the background of a div and i want to use rgba color in the browsers that support it but if they don't i would like to use rgb color as a fallback. I know how to achive this with plain css.

style="background: rgb(250, 0, 0); background: rgba(250, 0, 0, 0.5);"

Do you know how i could achieve this with jQuery .css()?

Upvotes: 0

Views: 1525

Answers (2)

kennypu
kennypu

Reputation: 6051

I believe if you use css(), it will overwrite the previous value for a property. If you want to do something like that, you can either set it manually via attr(), or you can use the callback function to see if it's IE or not, and set the value accordingly.

using attr()

$('element').attr('style','background: rgb(250, 0, 0); background: rgba(250, 0, 0, 0.5);');

using .css()

$('element').css('background', function() {
   // if you're using jquery version < 1.9
   if($.browser.msie && parseInt($.browser.version, 10) === 8) 
     return 'rgb(250, 0, 0);';
   else 
     return "rgba(250, 0, 0, 0.5)";
});

NOTE: if you're using jquery version 1.9 or above, $.browser is removed, so you will need to detect IE8 in a different way.

Upvotes: 4

Matt
Matt

Reputation: 5788

You have to pass css two args

$(this).css("background-color", "rgb(255, 255, 255)");

Upvotes: -3

Related Questions