0

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.

0

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Is it ever possible that top value will be returned anything other than decimal?
I tried this and it returned "NaN". Modal is an element passed into the funtion so ti is always pointing at the correct element.
Issue was I'd copied your code and forgot to remove the '' from the modal bit and mine is a declared variable. Stupidity. This is the solution I'll use because its a bit neater. Cheers
1

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.

2 Comments

The component is a popup though which is yet to be rendered, so this is returning 0.
@alex Please do you think you could help me with this question stackoverflow.com/questions/33054526/…
0

One solution involves regex replacement and cast to Number:

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

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

1 Comment

You are passing a string to replace(), not a regex, so the $ would be treated literally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.