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
+4.8k Chrome : How to block socketloop.com links in Google SERP?
+7.8k Golang : Convert(cast) io.Reader type to string
+9.6k Golang : Generate EAN barcode
+13k Golang : Convert int(year) to time.Time type
+27.1k Golang : Force your program to run with root permissions
+13.3k Golang : Handle or parse date string with Z suffix(RFC3339) example
+6.2k Golang : How to verify input is rune?
+9.9k Golang : Load ASN1 encoded DSA public key PEM file example
+10.6k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+8.3k Golang : Check from web if Go application is running or not
+7.1k Golang : Levenshtein distance example