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
+8.7k Golang : Bcrypting password
+72.5k Golang : How to return HTTP status code?
+48.7k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+8.6k Golang : Populate slice with sequential integers example
+13.9k Golang : Find location by IP address and display with Google Map
+9.3k Golang : Meaning of omitempty in struct's field tag
+11.7k Android Studio : Password input and reveal password example
+12.9k Golang : Convert IP version 6 address to integer or decimal number
+5k Golang : Experimenting with the Rejang script
+9.6k Golang : Command line file upload program to server example
+32.4k Golang : How to check if slice or array is empty?
+15.2k Golang : Send email and SMTP configuration example