views:

3297

answers:

6

I'm having a hard time picking up how to grab the dimensions of an element with jQuery. Here is my sample code:

$(document).ready(function() {
        var width = $("#image_1").width();
        var height = $("#image_1").height();
        document.write(width);
        document.write(height);
    });

Now of course I have an image with an id of #image_1. What happens when I try to run it is that it outputs two zeros. Not null twice, or undefined twice.

Thanks for the help from a javascript newb.

+1  A: 

You may get 0 for the width and height if the image is not visible. (That's what just happened to me.)

Updated: You can confirm that the image is added to the DOM by checking the length property of the jQuery object:

var inDOM = ($('#image_1').length > 0);
grammar31
It is visible, but jquery is basically choking and not showing the image. I was thinking along those lines. How can I tell if its actually inserted inside the DOM?
patricksweeney
+1  A: 

This works for me:

<head>
    <script type="text/javascript">
        function foo() {
            var image = document.getElementById("the_image");
            alert(image.offsetWidth);
            alert(image.offsetHeight);
        }
    </script>
</head>
<body onload="foo();">
    <img src="img.png" id="the_image">
</body>

This works as long as the image is not set to display: none; Using offsetWidth and offsetHeight also has the advantage of not requiring jQuery, at all.

Tyson
That returns undefined for both. Thanks for the help though!
patricksweeney
Can you post a link to a sample? It works for me (as shown in my edit above).
Tyson
I did something like the edit and it did work. I was messing around with something I have to do at work tomorrow. Basically I have a gallery that has a frame of 400x300, and most of the images are 395x295. But with some are smaller, and I want to center the small ones. Thanks!
patricksweeney
Hmmmm. I'm trying to accept your answer and it just shows a red box with the filename of the checkmark.....weird.
patricksweeney
+1  A: 

Perhaps this was a typo in your question, but is the ID of your image really "#image_1"? For your code to work, it should be just "image_1". The "#" is only used in the jquery selector to specify that the text following it is an ID.

tlianza
Interesting insight.
grammar31
+3  A: 

Even though you've already chosen an answer, I am typing this one so you understand why your prior code did not work.

jQuery's document.ready function fires before images are loaded. Use window.load instead...

$(window).load(function() {
  var width = $("#image_1").width();
  var height = $("#image_1").height();
  document.write(width);
  document.write(height);
});

For what it's worth, I think it is better to use jQuery for this task because of the inherent cross-browser functionality.

Josh Stodola
Agreed, I have since used something like that on jQuery as it does work better. Thanks!
patricksweeney
A: 

@Tyson

Please re-read the question again. Your solution finds the location, not the dimensions.