Get the width and height of an element if I haven't set it

I have an HTML element, but I did not specify the width or height (or sometimes I might not have defined the right or bottom, but I determined the width and height) in CSS.

Do you know how I can use JavaScript to get either the width or height of an element, either to the right or bottom, if I haven't set it?

#blockMenu { z-index: 0; padding: 10%; } 

If I do the following, I know that this will not work, because I have not set the width or right:

 var width = document.getElementById("blockMenu").offsetRight - document.getElementById("blockMenu").offsetLeft; 

Is there a way to determine the size and position of an element if I have not installed it?

If not, if I determine the width; how to get width of elements using javascript?

+4
source share
4 answers

Using javascript you can do

 document.getElementById("blockMenu").clientWidth; document.getElementById("blockMenu").clientHeight; 

With jQuery you can do

 $('#blockMenu').width(); $('#blockMenu').height(); 

Check out the working example http://jsfiddle.net/7jteV/2/

+2
source

if you have the ability to use jquery, you can use height () and width () .

0
source

You can also use jQuery

$ ("blockMenu"). width ()
$ ("BlockMenu"). Height()

http://api.jquery.com/width/
http://api.jquery.com/height/

0
source
 document.getElementById("blockMenu").style.width document.getElementById("blockMenu").style.height 

Gotta do the trick

-1
source

Source: https://habr.com/ru/post/1344710/


All Articles