I am trying to add some pixels from an element on my page. This is so I can create a new element and place it above it.
posy = document.getElementById('user').style.top + 20;
But it is returning “50px20”, I haven’t tried subtracting it yet, but what should I do to prevent it from returning ‘px’ in it?
Is there a way to do this in jQuery?
Thanks,
Alex.
What you are asking is a little confusing. You say you want the
topattribute, but this could be the position of the element on the page or the top value of its style. There is a distinct difference. It sounds like you want the position of the element because you are storing the results inposy. This is different than getting the top value of the CSS which will only exist if it has been defined in your stylesheet.To get the
topCSS value in jQuery you would useTo get the position of the element use jQuery’s
offset()orposition()methods. You probably would want offset.To fix your returned value for
pxoremtry parseInt.