Blog
HTML Textarea: Selection and caret position
2008-07-17 23:32
The last two days I spent a lot of time on a specific programming task for Moda. Finally I found a solution, using the internet as resource. Now I have succeeded, I want to do something in return by sharing that solution.

The task was that I had to find a good way to get the caret position and the selection inside a html textarea object and than use that info to edit the textarea value. I use this for my online UBB code editor (as you can see when you leave a comment).
The method I used before today didn't work well in Internet Explorer because the textarea sometimes completely loses focus.

After trying and searching the internet I finally found something usefull at http://blogs.nitobi.com/alexei/?p=173. This function returns the caret position and the start/end of a selection. After fixing a little bug, I could finish the editor.

Because the editor is integrated in the system I decided to make a dedicated example page, including only the needed javascript. You can find it here: http://www.tacojanosinga.nl/getsetselection.html.

I have tested this example in:
- Firefox 3
- Internet Explorer 6
- Internet Explorer 7
- Opera 9.5
- Safari 3.1
Tags:
No tags specified
View all news items
Download all news items as RSS