Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get width in pixels from element with style set with %?

I have this element:

<div style="width: 100%; height: 10px;"></div> 

I want to get it's width in pixels. I just tried this:

document.getElementById('banner-contenedor').style.width 

Which returns 100%. Is it possible to actually get the element's width in pixels with JavaScript?

like image 509
lisovaccaro Avatar asked Aug 13 '12 03:08

lisovaccaro


2 Answers

document.getElementById('banner-contenedor').clientWidth 
like image 186
xdazz Avatar answered Sep 17 '22 18:09

xdazz


You want to get the computed width. Try: .offsetWidth

(I.e: this.offsetWidth='50px' or var w=this.offsetWidth)

You might also like this answer on SO.

like image 38
GitaarLAB Avatar answered Sep 17 '22 18:09

GitaarLAB