Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 777
framomo86 Avatar asked Jan 22 '23 06:01

framomo86


1 Answers

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
like image 136
Pekka Avatar answered Jan 23 '23 20:01

Pekka