2

I am trying to get the width and height of an element. My element is a DIV with the id of "page".

The current code is not working as my div has a border and it allows my button to go out of the border.

I have tried the .offsetWidth as well as .clientwidth - Both returned null values.

var buttonState = document.getElementById("clickMe");
var maxArea = document.getElementById("page");
var pageWidth = maxArea.width;
var pageHeight = maxArea.height;
var screenWidth = 0;
var screenHeight = 0;
var buttonSize = 4;

2 Answers 2

1
window.getComputedStyle("page").getPropertyValue('font-size');
Sign up to request clarification or add additional context in comments.

Comments

0

document.getElementById("page").style.width

3 Comments

I am getting an error in console that says "Uncaught TypeError: Cannot read property 'style' of null"
That means the element isn't present when your JavaScript is run.
Double-check that your div has id="page" rather than class="page". Also it is a good idea to post the relevant part of the HTML as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.