Ryan Durrant
Ryan Durrant

Reputation: 1018

Get CSS value in JQuery

I'm using JQuery to try and get the value of the top from css.

However, where I have read a lot of things saying $('modal').css('top') will return a string "40" it is in fact retruning "40px".

Is there any way I can get this into an integer format. The component is not yet rendered on the page, I am trying to find the default position so I can set it using JS. This means that position().Top = 0 etc.

Upvotes: 0

Views: 81

Answers (3)

alex
alex

Reputation: 490233

You can use $.fn.position() to do that...

$('modal').position().top

If that doesn't work in your situation, you could use parseInt() or parseFloat(), because some browsers will give you fractional portions of pixels in some situations.

Upvotes: 1

bgoldst
bgoldst

Reputation: 35314

One solution involves regex replacement and cast to Number:

var topInt = Number($('modal').css('top').replace(/px$/,''));

http://jsfiddle.net/6n8xu0gf/1/

Upvotes: 0

Pratik Joshi
Pratik Joshi

Reputation: 11693

Use

parseInt($('.modal').css('top'), 10);

Working Fiddle

So it will not consider px or anything

I hope model is class or id , not Blank

Upvotes: 1

Related Questions