Awais Umar
Awais Umar

Reputation: 2075

Is it possible to append a css property with jquery?

Question : Is there any way to append some css property using jquery without changing/overriding the previous value?

For example : if i want to add !important to all of the color properties applied by my style sheet. Would it be possible to do it with jquery? rather than going through each of the css file and putting a !important in front of every color property?

Upvotes: 4

Views: 821

Answers (1)

Mark S
Mark S

Reputation: 3799

You can use the all selector ('*') together with each(), to loop through each element and apply the !important rule.

$('*').each(function(){
    var c = $(this).css('color');

    $(this).attr('style', 'color: '+c+' !important;');       
});

But be careful, the attr method will unset any previously set in-line style rules. You need to execute the above solution on top of everything first.

See this jsfiddle demo.

Upvotes: 3

Related Questions