-1

Specifically, a class or id within the css.

Would you use something similar to $(window).height()?

Bas Slagter
  • 9,831
  • 7
  • 47
  • 78
Northernlights
  • 109
  • 1
  • 10
  • `$(…)` is some function from a javascript framework. That's not a native javascript function – knittl Sep 07 '11 at 12:56

3 Answers3

3

It's not clear exactly what you want, but as a general rule all the styles applied to a given element can be accessed in JavaScript using something like:

element.style.<property-name>

So using native JavaScript you can do:

var elemStyles = document.getElementById("someId").style;
var styleWidth = elemStyles.width;

Assuming at least one element with a given CSS class and a framework that can select elements by class, you can similarly do:

var elemStyles = $(".someClass")[0].style;
var styleWidth = elemStyles.width;

Or depending upon what (if any) JavaScript framework you are using, there may be specialized methods that you can use to access/inspect various CSS attributes for a given element.

Note that any of these methods will bring back all the styles applied to the element, whether they are coming from the CSS file, from inline CSS declarations, or added programmatically by a script on the page. If you want to get just the styles inherited from the CSS file, then things get a bit trickier.

aroth
  • 54,026
  • 20
  • 135
  • 176
  • I'd like to change the amount of pixels a div animates left for (using jquery), depending on the width of the div next to it (which changes), hence the need to retrieve with width and insert it into a variable. Or is there a better way? – Northernlights Sep 07 '11 at 13:17
  • @NorthernLights - Using jQuery, I'd recommend just using something like `$("#otherElemId").width()` to get the width. – aroth Sep 07 '11 at 22:14
1

yes its possible

if you would like to receive other css properties check this out http://api.jquery.com/css

you would do somethig like this

var cssvalue = $(selector).css(propertyName);
Breezer
  • 10,410
  • 6
  • 29
  • 50
0

This will probably help you, too. Esp. if you want to do it without jQuery: How do you read CSS rule values with JavaScript?

Community
  • 1
  • 1
Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165