Suck At Coding
Suck At Coding

Reputation: 27

Still can't get the original height of an HTML element using JavaScript

I have problem where I have an element like

div#myElement
{
    height: 600px;
}

and in my JavaScript I need to get the value 600 (so that it's not a magic number floating around many places of my code). When call that $("#myElement').height() I get a decimal, something like 599.something. Someone suggested that I instead use $('#myElement').css('height'). When I did that, it still returned a decimal value. Click here http://jaminweb.com/snake_TEST_PHP.php and see the alert that comes up. That's from the line

        var SH = $("#snakediv").css("height");
        alert(SH);

Any help greatly appreciated.

Upvotes: 1

Views: 48

Answers (1)

Paul S.
Paul S.

Reputation: 66334

I get a decimal, something like 599.something

This is because you've zoomed your page (Ctrl + mousewheel). This means that pixels are being scaled, and the calculation to work out sizes has some error introduced, resulting in your almost-but-not-exactly value of e.g. 599.5454545021057 at 110%.

Reset your zoom to 100% and you'll get the expected 600

Upvotes: 7

Related Questions