Gabriela Ticu
Gabriela Ticu

Reputation: 31

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.

Upvotes: 3

Views: 361

Answers (1)

Martin Osusky
Martin Osusky

Reputation: 845

Use .offsetHeight instead of .style.height:

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

Upvotes: 1

Related Questions