Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get text width in pixels, using JavaScript or jQuery? [duplicate]

I want to get textarea text width. How do I get text width in pixels?

I do not want the text length.

like image 883
user1241761 Avatar asked Jul 17 '26 08:07

user1241761


1 Answers

Simply wrap the text in a (invisible) span and use jQuerys .width() to get the width.

Note that you should not, like others said, use a div as it's a block-element - and depending on where/how you insert that, a block-element may auto-expand to the full available width and give bad results.

like image 88
oezi Avatar answered Jul 18 '26 23:07

oezi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!