Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a CSS value from external style sheet with Javascript/jQuery

Is it possible to get a value from the external CSS of a page if the element that the style refers to has not been generated yet? (the element is to be generated dynamically).

The jQuery method I've seen is $('element').css('property');, but this relies on element being on the page. Is there a way of finding out what the property is set to within the CSS rather than the computed style of an element?

Will I have to do something ugly like add a hidden copy of the element to my page so that I can access its style attributes?

like image 540
Acorn Avatar asked Apr 25 '10 09:04

Acorn


People also ask

How can check CSS property value in JQuery?

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

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.

How do you retrieve a CSS property value of an element?

First, you need to select the element with querySelector . Then, you use getComputedStyle to get the element's styles. If you log style , you should see an object that contains every CSS property and their respective values. You can also see this object in Chrome's and Firefox's dev tools.


2 Answers

With jQuery:

// Scoping function just to avoid creating a global  (function() {      var $p = $("<p></p>").hide().appendTo("body");      console.log($p.css("color"));      $p.remove();  })();
p {color: blue}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Using the DOM directly:

// Scoping function just to avoid creating a global  (function() {      var p = document.createElement('p');      document.body.appendChild(p);      console.log(getComputedStyle(p).color);      document.body.removeChild(p);  })();
p {color: blue}

Note: In both cases, if you're loading external style sheets, you'll want to wait for them to load in order to see their effect on the element. Neither jQuery's ready nor the DOM's DOMContentLoaded event does that, you'd have to ensure it by watching for them to load.

like image 53
karim79 Avatar answered Sep 17 '22 15:09

karim79


Normally you should be let the browser apply all the rules and then ask the browser for the results, but for the rare case where you really need to get the value out of the style sheet you can use this: (JSFiddle)

function getStyleSheetPropertyValue(selectorText, propertyName) {     // search backwards because the last match is more likely the right one     for (var s= document.styleSheets.length - 1; s >= 0; s--) {         var cssRules = document.styleSheets[s].cssRules ||                 document.styleSheets[s].rules || []; // IE support         for (var c=0; c < cssRules.length; c++) {             if (cssRules[c].selectorText === selectorText)                  return cssRules[c].style[propertyName];         }     }     return null; }  alert(getStyleSheetPropertyValue("p", "color")); 

Note that this is pretty fragile, as you have to supply the full selector text that matches the rule you are looking up (it is not parsed) and it does not handle duplicate entries or any kind of precedence rules. It's hard for me to think of a case when using this would be a good idea, but here it is just as an example.

like image 29
Old Pro Avatar answered Sep 19 '22 15:09

Old Pro