Emphasis on "seems" (BTW: is your keyboard broken?svasti wrote:Seems to work!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
![Wink ;)](./images/smilies/icon_e_wink.gif)
It doesn't work in IE 8. After a few new lines, the box doesn't grow anymore. I tracked the problem down to
Code: Select all
textarea.style.height = "auto";
Furthermore the setting of `offset' (which may be named `border' for clarity, assuming the textarea doesn't have a horizontal scrollbar) don't seem to require the special clause for Opera (at least clientHeight works fine in 12.14).
I was not not sure about the used events to trigger the resizing. As I learned `input' is part of the HTML5 specification, so it should be fine to use it. A fallback to `keypress' (I would prefer that over `keyup' to cater for key repeats) might not harm. For IE<=8, which doesn't know about `input', `propertychange' seems to be a reasonable alternative.
But still there remained the problem with the height="auto", which shouldn't be worked around by browser sniffing. So I read again the alistapart article about the `mirror' variant:
Aha! So probably the `mirror' element solution is preferable as offsetHeight is standardized. Now I wanted to look up, where, and I found http://www.w3.org/TR/cssom-view/. There HTMLElement.offsetHeight is going to be standardized; and in the same document I found Element.scrollHeight! So prefering offsetHeight over scrollHeight because of standardization is nonsense.The required height is calculated based on the scrollHeight attribute, which is not specified in any W3C spec (it was introduced by IE) and thus unsurprisingly has subtle differences between implementations, requiring an inelegant and brittle lump of browser-sniffing code.
And as I prefer to avoid a mirror element, if not absolutely necessary, I've investigated the scrollHeight issue further. In most browsers (Chrome, FF, Opera) after setting:
Code: Select all
textarea.style.height = (textarea.scrollHeight + border ) + "px";
Christoph