jQuery and hidden items

Posted by on March 29, 2008

Doing some resent work with jQuery a div that is initially hidden from the user, I went round in cirlces when I kept getting nothing when trying to get the div’s margin information via:

$('#myitem').offset()

I kept getting zero returned for the top and left margins. What I needed to do was to show() the div:

$('#myitem').show().offset().hide()

This allows me to get the margins, but ultimately keeps the div hidden from the user.

Hopefully, documenting this here will help someone if they run into in the future, or it’ll help me the future me if I ever forget.