Search code examples
javascriptjquerycsstextareawidth

How to get text width inside textarea? (javascript/jquery)


Is it possible to get width of text inside a textarea element?

I don't want $('textarea').val().length per se, as it doesn't depend on font-size.

I would prefer the absolute width of the text inside.


Solution

  • It's well possible this information is impossible to get hold of.

    Workaround idea:

    • Create a span element with the font size settings of the textarea
    • Give the span element the input's value using .text(value)
    • Measure the span's width