Javascript : How to get width and height of a div?
Problem :
You have a <div>
and you want to get the dimension of the <div>
such as height and width. You want to ensure that the <div>
dimension is not being tempered by third-party browser's add-ons.
How to do that?
Solution :
Imagine you have a <div id="box">
with id = box. You can get the dimension with getElementById()
function to get the offsetWidth and offsetHeight properties.
For example :
the HTML :
<div id="box">
fill up something here
the more you fill... the higher
the height will be
<br>
<br>
</div>
and the Javascript :
var element = document.getElementById('box');
var height = element.offsetHeight;
alert(height);
var width = element.offsetWidth;
//alert(width)
See demo at http://codepen.io/anon/pen/KdVXLw
IF the height or width is below certain pixels, you can either prompt alert to your website visitors or redirect them to another page.
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+18.2k Golang : Get path name to current directory or folder
+5.8k Golang : Find change in a combination of coins example
+4.6k JavaScript : Rounding number to decimal formats to display currency
+17.2k Golang : When to use init() function?
+46.4k Golang : Encode image to base64 example
+9.3k Golang : How to protect your source code from client, hosting company or hacker?
+7.2k Golang : Null and nil value
+7.7k Golang : Test if an input is an Armstrong number example
+25k Golang : Create PDF file from HTML file
+20.3k Golang : Check if os.Stdin input data is piped or from terminal
+5.7k Fix yum-complete-transaction error
+7.3k Golang : File system scanning