Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery find image height not working

I'm trying to use jQuery to find the image height of the first "grand children" of a container, then set the container to that height. But, I can't seem to pull the image height attribute - getting the src works. Any ideas? Is it just trying to pull the heights via CSS? How do i get the "real heights" I can't input the width and height in img tag - so that's not an option ;(

 $(document).ready(function() {
       var imageContainer = '#image-container';


       var imageSrc = $(imageContainer).children(':first').children(':first').attr('src'); // control test
       var imageHeight = $(imageContainer).children(':first').children(':first').height(); // have also tried attr('height')

       alert(imageSrc + ' ' + imageHeight);

    });


    <div id="image-gallery">
      <div id="image-container">    

        <div class="slide">
          <img src="test1.jpg" alt="test1" />
          <p>
            <strong>This is a test 1</strong>
          </p>
        </div>

        <div class="slide">
         <img src="test2.jpg" alt="test2" />
         <p>
           <strong>This is a test 2</strong>
         </p>
       </div>

      </div>
    </div>
like image 370
stapler Avatar asked Jan 27 '10 22:01

stapler


People also ask

How can get image width and height in jquery?

width(); alert(imageWidth); var imageHeight = Imgsize. height();

How do you get the image width and height using JS?

Answer: Use the JavaScript clientWidth property You can simply use the JavaScript clientWidth property to get the current width and height of an image. This property will round the value to an integer.

How do you find the height and width of an image in react?

To get the dimensions of image with React, we can get it from the load event handler of the image. We define the onImgLoad function that gets the image from the target property. Then we destructure the offsetHeight and offsetWidth properties from the image. Next, we log the height and width with console.


3 Answers

From the API docs:

In cases where code relies on loaded assets (for example, if the dimensions of an image are required), the code should be placed in a handler for the load event instead.

So instead of executing your script in $(document).ready(); use $(window).load(); or better yet, $(image).load();. For example:

$(document).ready(function() {
    var imageContainer = '#image-container';

    $(imageContainer).children(':first').children(':first').load(function() {
        var imageSrc = $(this).attr('src'); // control test
        var imageHeight = $(this).height(); // have also tried attr('height')

        alert(imageSrc + ' ' + imageHeight);
    });
});
like image 134
Joel Avatar answered Sep 21 '22 22:09

Joel


you should wait until your image is loaded - something along the line of

 $('#imageContainer').children(':first').children(':first').load(function() {
     alert($(this).height());
 });
like image 27
roman Avatar answered Sep 18 '22 22:09

roman


The reason that you can't get the size of the images is that they don't have any size yet. The code runs after the page has loaded but before the images has loaded.

Run the part of the code that needs the image size after all elements in the page are loaded:

$(window).load(function () {
  ...
});
like image 33
Guffa Avatar answered Sep 20 '22 22:09

Guffa