31

How do I retreive the line-height of an element without the "px"?

Using this I get the full line-height value including the px.

$('#example').css('line-height');
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
cusejuice
  • 10,285
  • 26
  • 90
  • 145
  • possible duplicate of [Get a number for a style value WITHOUT the "px;" suffix](http://stackoverflow.com/questions/8690463/get-a-number-for-a-style-value-without-the-px-suffix) – Felix Kling May 08 '12 at 22:14

4 Answers4

69

Parse it as an integer with parseInt

parseInt($('#example').css('line-height'), 10);

Outputs:

18

As an integer. The other solutions maintain the String type.

EDIT

For values that may contain decimal points, you can use parseFloat()

parseFloat($('#example').css('line-height'));

Outputs:

18.4

Kyle Macey
  • 8,074
  • 2
  • 38
  • 78
  • 2
    Keep in mind that if your original `line-height` was set as a percentage and not pixels or ems/rems, using `parseInt()` will trim off decimals in your calculated `line-height`... – nickb Jan 06 '14 at 05:25
13

Just replace the px with ''.

$('#example').css('line-height').replace('px', '');

Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
3

save it into a variable an then make a replace

var aux = $('#example').css('line-height').replace('px', '');
Jorge
  • 17,896
  • 19
  • 80
  • 126
-3

In coffeescript

getElementProperty = (el, property) -> 
  val = el.css(property).replace "px" , "" 

  parseInt val


getElementProperty $("#myElement"), "line-height"

This should to it !

htatche
  • 693
  • 3
  • 17