Get text width with element offsetWidth - Javascript DOM

Javascript examples for DOM:Element offsetWidth

Description

Get text width with element offsetWidth

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from  w  w w  . j av  a  2s . co  m
var textNode = document.getElementById('text-to-measure');
var width = textNode.offsetWidth
document.getElementById('answer').innerText = width
    }

      </script> 
   </head> 
   <body> 
      <span id="text-to-measure">WWWWWWWWWW</span> 
      <div id="answer"></div>  
   </body>
</html>

Related Tutorials