A web developer’s diary

April 11, 2007

Javascript: Positioning the cursor at the beginning of a textarea

Filed under: Javascript — Celia @ 7:13 am

I had been using the textarea.focus() function and saw that it behaves differently in different browsers. If there is no content in the textarea, all the browsers behave the same and they set the cursor position at the beginning of the textarea.

However, if there is some text contained in the textarea, IE positions the cursor at the end whereas safari positions the cursor at the beginning. After some frantic google searching, I found the below javascript would fix this discrepancy.

if (replybox.setSelectionRange )

This works fine in all the browsers. Let me know if this helped you.

Blog at WordPress.com.