Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a style attribute from a CSS class by javascript/jQuery?

How can I access a property from a CSS class by jQuery? I have a CSS class like:

.highlight {      color: red;  } 

And I need to do a color animation on an object:

$(this).animate({     color: [color of highlight class] }, 750); 

So that I can change from red to blue (in the CSS) and my animation will work in accordance with my CSS.

One approach would be to place an invisible element with the highlight class and then get the color of the element to use in the animation, but I guess this is a very, very bad practice.

Any suggestions?

like image 492
robsonrosa Avatar asked Jun 06 '13 15:06

robsonrosa


People also ask

How do I retrieve the properties of a CSS element?

Get a CSS Property Value You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css("propertyName");

How do you call a CSS class in JavaScript?

className = "MyClass"; In the css class MyClass you can set the background image.. You can create another css class with different background image and set it according to the conditions.. Show activity on this post.

What is the use of CSS () method in jQuery?

jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.

Can we change CSS property value using JavaScript or jQuery?

You can change CSS using the jQuery css() method which is used for the purpose of getting or setting style properties of an element. Using this method you can apply multiple styles to an HTML all at once by manipulating CSS style properties.


1 Answers

I wrote a small function that traverses the stylesheets on the document looking for the matched selector, then style.

There is one caveat, this will only work for style sheets defined with a style tag, or external sheets from the same domain.

If the sheet is known you can pass it in and save yourself from having to look in multiple sheets (faster and if you have colliding rules it's more exact).

I only tested on jsFiddle with some weak test cases, let me know if this works for you.

function getStyleRuleValue(style, selector, sheet) {     var sheets = typeof sheet !== 'undefined' ? [sheet] : document.styleSheets;     for (var i = 0, l = sheets.length; i < l; i++) {         var sheet = sheets[i];         if( !sheet.cssRules ) { continue; }         for (var j = 0, k = sheet.cssRules.length; j < k; j++) {             var rule = sheet.cssRules[j];             if (rule.selectorText && rule.selectorText.split(',').indexOf(selector) !== -1) {                 return rule.style[style];             }         }     }     return null; } 

example usage:

var color = getStyleRuleValue('color', '.foo'); // searches all sheets for the first .foo rule and returns the set color style.  var color = getStyleRuleValue('color', '.foo', document.styleSheets[2]);  

edit:

I neglected to take into consideration grouped rules. I changed the selector check to this:

if (rule.selectorText.split(',').indexOf(selector) !== -1) { 

now it will check if any of the selectors in a grouped rules matches.

like image 166
rlemon Avatar answered Sep 25 '22 10:09

rlemon