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
+9.2k Golang : How to get quoted string into another string?
+16.6k Golang : Simple client server example
+9.6k Golang : Get UDP client IP address and differentiate clients by port number
+5.1k Golang : Experimenting with the Rejang script
+23.9k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+16.1k Golang : How to make a file read only and set it to writable again?
+10.7k Golang : Clean formatting/indenting or pretty print JSON result
+7.4k Golang : Generate Datamatrix barcode
+4.8k Unix/Linux : Get reboot history or check when was the last reboot date
+8.6k Golang : Turn string or text file into slice example
+9k Golang : Print how to use flag for your application example
+8.1k Golang : Play .WAV file from command line