0

It's trivial splitting a textarea by newline using split('\n').

Can you split a proportional-font textarea so that each wrapped line is a separate element in the array?

Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79
  • I don't think you can... – Karl-André Gagnon Sep 04 '14 at 13:17
  • @Karl-AndréGagnon, I was afraid of that. HTML5 adds the `wrap="hard"` attribute, but it's unfortunate that the browser doesn't expose the breaks until after form submission. There are interesting techniques for detecting soft wraps at http://stackoverflow.com/questions/4719777/finding-line-breaks-in-textarea-that-is-word-wrapping-arabic-text – Rick Hitchcock Sep 05 '14 at 14:14
  • That's a nice hack, does it work? – Karl-André Gagnon Sep 05 '14 at 14:31
  • I haven't tried that particular technique, but I came up with a similar one last night using a `div` to hold the `textarea` contents. It works fairly well in Chrome if I set identical properties on both elements for `font`, `letter-spacing`, `padding`, `width`, and `word-spacing`; and if I set the `div`'s `word-wrap:break-word`. I also replace `\n`s with `
    `s, and all double-spaces with space+ . However, I can't seem to come up with a style that will make IE wrap a `div` like it does a `textarea`.
    – Rick Hitchcock Sep 05 '14 at 14:43
  • Another question I have, which may be impossible, is can we get the text within `getClientRects()`? This would greatly simplify the split, if only I could duplicate a `textarea`'s style in a `div`. – Rick Hitchcock Sep 05 '14 at 14:43
  • I really don't know, i'm sorry. On the `div`, I would be able to to get the line break, but but it would require to loop over all character, which is not optimal. – Karl-André Gagnon Sep 05 '14 at 14:51
  • Agreed. I'm not hopeful for a `getClientRects()` -> text solution, so I'll experiment with the technique I linked to. Thanks! – Rick Hitchcock Sep 05 '14 at 14:55

0 Answers0