This is probably a really simple question, but how do I go about getting the right offset of an element in jQuery?
I can do:
$("#whatever").offset().left;
and it is valid.
But it seems that:
$("#whatever").offset().right
is undefined.
So how does one accomplish this in jQuery?
Thanks!!
There's a native DOM API that achieves this out of the box —
getBoundingClientRect
:Getting the anchor point of a
div/table (left) = $("#whatever").offset().left;
- ok!Getting the anchor point of a
div/table (right)
you can use the code below.Alex, Gary:
As requested, here is my comment posted as an answer:
Thanks for letting me know.
In pseudo code that can be expressed as:
The right offset is:
The window's width MINUS
( The element's left offset PLUS the element's outer width )
Brendon Crawford had the best answer here (in comment), so I'll move it to an answer until he does (and maybe expand a little).
Reference: .outerWidth()
Maybe I'm misunderstanding your question, but the offset is supposed to give you two variables: a horizontal and a vertical. This defines the position of the element. So what you're looking for is:
and
If you need to know where the right boundary of your element is, then you should use: