Author Archive

Rewind caret in a non-empty <textarea>

Sunday, February 19th, 2006

When you TAB into a non-empty <textarea> the caret is usually placed after the last character. Which is not always ok. This JavaScript code deals with this problem for all modern browsers. IE variant needs onfocus and onbeforedeactivate event handlers which are installed with add_handler() function.
function add_handler(element, event_type, func) {
    if (element.attachEvent) {                  // IE
        element.attachEvent(’on’ + event_type, [...]

Keep on coding