tags:

views:

136

answers:

2

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!!

A: 
var $whatever        = $('#whatever');
var ending_right     = $whatever.offset().left + $whatever.outerWidth();

Reference: .outerWidth()

jAndy
i think need to add them and then minus 1. if the width is 2, then left is at 10, and right is not at 12 but 11.
動靜能量
+1  A: 

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:

$("#whatever").offset().left

and

$("#whatever").offset().top

If you need to know where the right boundary of your element is, then you should use:

$("#whatever").offset().left + $("#whatever").outerWidth()
Greg