0

These are my codes:

a.html

<div class="pic-box">
    <img src="a.jpg" alt="" />
</div>

index.html

<div class="get-box"></div>

index.js

const pageUrl = './pages/a.html',

fetch(url).then(function(response){
    if (response.ok) {
        return response.text();
    }
}).then((data) => {
    $('.get-box').html(data);
    const imageHeight = $('.get-box img').height();
    console.log(imageHeight);
});

I want to get the height of the image, but it showes me it 0. How could I get the true height?

Mark
  • 27
  • 1
  • 9

1 Answers1

0

You should append a.html source to <div class="get-box"></div> element or replace:

a.html

<div class="pic-box">
    <img src="a.jpg" alt="..." onload="imageLoaded(this)" />
</div>

If you use jQuery:

function imageLoaded(element){
    const imageHeight = $(element).height();
    console.log(imageHeight);
}

const url = './pages/a.html';

$.ajax({ url: url })
  .done(function(html) {
    // Append
    $(".get-box").append(html);

    // or Replace
    // $(".get-box").replaceWith(html);           
  });