views:

152

answers:

3

Using jQuery, how can I make a textarea that automatically adds a new line when inserting text when the cursor is near the end.

The width of the textarea is dynamically done via CSS (e.g., #myTextArea { width: 80%; }) so I can't do anything like counting the characters in each line.

I don't need a non-JS solution since the content of the textarea is only ever seen by JavaScript anyways.

+1  A: 

You can get the calculated pixel width of the text area and the textarea's padding. You can look up the average (or mode, or whatever pleases you) horizontal width of your font. You can make a guess at the kerning. See what I'm getting at? (hint: calculate the line length in characters)

You mentioned counting characters, so I assume you already know how to do that. :-)

S..
If I change the font size of my IE window then all bets are off me thinks.
griegs
A: 

This is a really nasty problem to try and solve. I'd be going down either 1 of 2 pahts. 1 just use the text area as is or 2 using a nice jQuery editor which makes the text look nice.

griegs
+1  A: 

maybe you can help this, jquery autoresize

eos87
Did not solve the question directly, but solved the greater problem.
MiffTheFox