Published on

jQuery and hidden items

Authors

Doing some resent work with jQuery a div that is initially hidden from the user, I went round in circles 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.