0

So I've got this element which I need to get the width() and height() of, but it keeps returning 0, but when i enter $('#lightingData').width() in the console, it gives me the right values. I think i need to use some other wrapper function, I have tried $(document).ready(function() {...}); and $(window).load(function(){...});

Are there other wrapper functions which will wait even longer before executing?

8
  • window load should be more than enough for a static element. Are you adding the element with javascript? Commented Apr 11, 2013 at 17:49
  • Can you post a complete example and a jsFiddle? Commented Apr 11, 2013 at 17:49
  • @j08691, I may or may not work on creating an example, it would take awhile, the site is a mess and is also private. Commented Apr 11, 2013 at 17:50
  • Is the element, by any chance, an image or does it contain an image? Commented Apr 11, 2013 at 17:51
  • 1
    @TheBrain - YIKES! (but as a last resort, yes) Commented Apr 11, 2013 at 17:57

3 Answers 3

2

if the #lightningData element has display:none when the onload is triggered and your code executed, widht() and height() will return 0. You can explicitly call .show() on the element before checking for dimensions

 $('#lightingData').show().width();
Sign up to request clarification or add additional context in comments.

3 Comments

This is only useful if the element is, in fact, loaded at the time this is called
yes. that's a given. width of nothing can't be greater than 0.
You can also call .hide() immediately at the end to restore the element to its previous state.
2

One of the things you can try is explicitly waiting for that particular element to be loaded before trying to call .width() and .height().

Try this:

$('#lightingData').load(function() { 
     ...height and width manipulation here...
     ...don't forget about scope...
});

You should put this inside your $(document).ready(...), as you will want the document to at least know what $('#lightingData') is. Because you are getting 0 for the height and width and not errors, I can assume that they are at least found, before the methods are called.


UPDATE

Per the comments added by @Ian, I want to point out that this will only work for certain types of elements (URL, IFrames, Images/media, etc. - basically anything that needs to load its content). I am not going to update the code, since what I have above is correct if you are using it for one of these kinds of elements, and would be silly if you are using it for another. If you are using it for both, you need only add a condition/filter (based on your implementation) to determine if this code should be bound to that particular element.

7 Comments

Why are you wrapping a jQuery object in another?
@Ian - haha that was a mistake, thanks! I think I was going to type the selector, initially, but then came to my senses and went with copy-pasta. Chalk another one up for the reasons to be careful of Copy-Paste!
Also, checking inside of $(window).on("load", function () {}); should be more than enough, as all images will have loaded - then you don't need the load event of the image. The problem is more likely that the element is hidden and width/height can't be calculated.
@Ian - I completely agree with you, which is why I asked if there were images involved in the comment. He said that there were not, but, considering we cannot actually see any of the source, as it is private, I figured it would be best to at least ensure that the base-cases were all covered :)
Good point :) And just as a note, the load event only fires for certain elements...ones associated with an URL - images, scripts, frames, iframes, and the window object. So hopefully the element is one of those types :)
|
0

If you are 100% sure that the width is still 0 at the point of $(document).ready()

try this:

$(function () {
    $('#lightingData').show(function(e) {
        var width = $(this).width();
        //do something
    });
});

Or, something stupid but will work for 90% of the case:

$(function() {
    setInterval(function () {
        var width = $('#lightingData').width();
        //do something
    }, 1000);
});

This one will delay the action for 1 sec after $(document).ready().

Without the exact scenario it's hard to give a specified solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.