How to get the height of a div after its content is loaded?

user5716144

im trying to get the size of an <img> in the <window> by checking width and height of its container.

i used .load() to wait until the <img> is loaded.

On pageload the size is still 0.

I only get the correct height on resize.

What is wrong here?

EDIT: to get the correct size i added .contents()

$(document).ready(function(){
    $('img').load(function(){   
        function size(){
            var height  = $('#size').contents()height();
            var width   = $('#size').contents().width();
            $('#size').find('p.size').remove();
            $('#size').append('<p class="size">' + height + '-' + width + '</p>');
        };
        $(window).load(size);
        $(window).resize(size);
    });
});
pwolaq

you define a function, but you don't invoke it

place size(); after $(window).resize(size);

EDIT: whole code with some improvements

$(document).ready(function(){
    function size(){
        var height = $('#size').height();
        var width = $('#size').width();
        $('#size').find('p.size').remove();
        $('#size').append('<p class="size">' + height + '-' + width + '</p>');
    }

    $(window).load(size);
    $(window).resize(size);
    $('img').load(size);
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the height of a div after its content is loaded?

From Dev

How to keep div height fixed during changing its content?

From Dev

How to get container height after I loaded the html

From Dev

How to get container height after I loaded the html

From Dev

Get Image height before its fully loaded

From Dev

How to get the content of the bottom element of a flexbox to be 100% height its container

From Dev

div height auto adjust to loaded page content within

From Dev

How to make outer div expand to the height of all its dynamically generated content?

From Dev

How do I make a div height or position adapt to its content's relative position

From Dev

How to fit DataGridView width and height to its content?

From Dev

How to make UITextView in section header adjust its height to its content

From Dev

div height adjusting doesn't include margin of its content

From Dev

How to cover a div with an image independently of its height?

From Dev

How to set height of div match its container

From Dev

Get content of <div> knowing its id

From Dev

How to get div height based on image height?

From Dev

How to get Dynamically loaded content using HtmlAgilityPack

From Dev

How to make sidebar with same height as the content div?

From Dev

How to make the height of content div proportional?

From Dev

How to make this content div 100% of available height?

From Dev

How to make sidebar with same height as the content div?

From Dev

jquery get visible html div content loaded by ajax?

From Dev

How to get view height considering its parent has infinite height?

From Dev

how to free a pointer after it returned its content

From Dev

How can we adjust the height of a parent element depending on its content?

From Dev

How to get inner HTML content height

From Dev

How to get page content height using pdfbox

From Dev

How do I disable a cascading kendo combobox after its loaded?

From Dev

How to get access to data only after they are loaded?

Related Related

  1. 1

    How to get the height of a div after its content is loaded?

  2. 2

    How to keep div height fixed during changing its content?

  3. 3

    How to get container height after I loaded the html

  4. 4

    How to get container height after I loaded the html

  5. 5

    Get Image height before its fully loaded

  6. 6

    How to get the content of the bottom element of a flexbox to be 100% height its container

  7. 7

    div height auto adjust to loaded page content within

  8. 8

    How to make outer div expand to the height of all its dynamically generated content?

  9. 9

    How do I make a div height or position adapt to its content's relative position

  10. 10

    How to fit DataGridView width and height to its content?

  11. 11

    How to make UITextView in section header adjust its height to its content

  12. 12

    div height adjusting doesn't include margin of its content

  13. 13

    How to cover a div with an image independently of its height?

  14. 14

    How to set height of div match its container

  15. 15

    Get content of <div> knowing its id

  16. 16

    How to get div height based on image height?

  17. 17

    How to get Dynamically loaded content using HtmlAgilityPack

  18. 18

    How to make sidebar with same height as the content div?

  19. 19

    How to make the height of content div proportional?

  20. 20

    How to make this content div 100% of available height?

  21. 21

    How to make sidebar with same height as the content div?

  22. 22

    jquery get visible html div content loaded by ajax?

  23. 23

    How to get view height considering its parent has infinite height?

  24. 24

    how to free a pointer after it returned its content

  25. 25

    How can we adjust the height of a parent element depending on its content?

  26. 26

    How to get inner HTML content height

  27. 27

    How to get page content height using pdfbox

  28. 28

    How do I disable a cascading kendo combobox after its loaded?

  29. 29

    How to get access to data only after they are loaded?

HotTag

Archive