jQuery – Get the CSS value without the px unit

When we use jQuery to retrieve the CSS values such as width and height, the unit px is included.

For example, if you have a div which is 100px wide.

<div id="eureka" style="width: 100px;"></div>


 

Call .css() to get the width value.

$('#eureka').css('width'); // Return '100px'

 

But most of the time we would like to have a numeric value rather than a string. Here comes the numeric solution.

parseFloat($('#eureka').css('width')); // Return 100

 

You don’t need to worry about the unit em. This is because .css() must return the value in unit px.

Done =)

Reference:

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.