Reputation: 19
How do I find the width of an element?
I have maxwidth set to 100px & width set to "auto". I'm attempting to find its exact width dynamically which could be <100px.
I tried these options: element.offSetWidth, element.style.width, created a region through Dom.getRegion & pulled width..
However, all the above options return 100px as width.
Any non-jquery based solution please?
-Vidi
Upvotes: 1
Views: 1613
Reputation: 845
add a float or make it inline
<style type="text/css">
#test_max_width{
max-width : 100px;
float:left;
}
</style>
<div id="test_max_width">s</div>
<input type="button" value="more" id="addcontent"/>
<script type="text/javascript">
var output = document.getElementById( "test_max_width" );
document.getElementById( "addcontent" ).onclick= function ( ) {
output.innerHTML += output.innerHTML;
console.log ( output.offsetWidth );
};
</script>
Upvotes: 0
Reputation: 371
document.getElementById(ID_OF_ELEMENT).offsetWidth should work
Upvotes: 0