I’m trying to access the width of certain elements by using jQuery’s .width() function, but sometimes the return value is 0 when I don’t expect it. Is this a known issue? Does an element have to be visible for this to work, perhaps? If there are limitations, how can I work around them?
I’m trying to access the width of certain elements by using jQuery’s .width() function,
Share
If you’re dealing with images, make sure to run the code in
$(window).load()rather than$(document).ready(), like this:Otherwise, make sure the element is visible, hidden elements have no
.width()or.height()(it’ll return 0 for both).