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:

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s