Browser frustrations

Earlier on I was writing some code that inserted some text into a textarea on a webpage. After the text has been inserted I needed the cursor to be at the end of the textarea so that the user could then type more text in afterwards without any messing around.

I thought it would be a simple matter, but it turned out to be quite frustrating.

Needed for all
var t = document.getElementById('add-comment');

Gecko (Firefox, Mozilla, etc.)
t.focus();

Webkit (Chrome, Safari, etc.)
t.selectionStart(t.value.length);
t.selectionEnd(t.value.length);

Internet Explorer
t.setSelectionRange(t.value.length, t.value.length);
t.focus();

Is there some standard way of doing this that I’m unaware of? I think jQuery would be overkill for this one purpose.

Leave a Reply

Your email address will not be published. Required fields are marked *