Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the function element.style.height in JavaScript may not work?

I cannot get the object's height in js code. My HTML code:

<!DOCTYPE html>
<html lang="en">
    <body>
      <object onload="loadMarkers()" type="image/svg+xml" data="WorldMap.svg" id="worldMap"></object>
      <script src="project.js"></script>
    </body>
</html> 

JS code (the name of the file in HTML is right, I checked it):

function loadMarkers() {
    var h = document.getElementById("worldMap").style.height;
    console.log(h);
}

The console output is an empty line. If I write

var h = Number(document.getElementById("worldMap").style.height);
console.log(h);

the output is 0.

like image 552
Gabriela Ticu Avatar asked Dec 20 '25 09:12

Gabriela Ticu


1 Answers

Use .offsetHeight instead of .style.height:

var h = Number(document.getElementById("worldMap").offsetWidth);
console.log(h);
like image 187
Martin Osusky Avatar answered Dec 22 '25 00:12

Martin Osusky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!