Wednesday, April 18, 2007

Javascript to move cursor to the end of a textbox

function setCursorAtEnd(sTextboxID) { var oTextbox = document.all.item(sTextboxID); if (oTextbox .createTextRange) { var r = (oTextbox.createTextRange()); r.moveStart('character', (oTextbox.value.length)); r.collapse(); r.select(); }}

8 comments:

Anonymous said...

very useful.. any chance you know how to do this for Gecko-based browsers?

Anonymous said...

yeahhhh. that's it. thank you dudeeeeeee.

Anonymous said...

Thank you.

Anonymous said...

a cheap way to do this (works on ie7, not tried others yet) :

myEl.focus();
myEl.value = myEl.value;

Anonymous said...

great post. the line following function call is not really necessary though.

r.collapse();

as the the r.select(); function call puts the focus at the end of the textrange.

Anonymous said...

what attribute do you use in the textarea tag.

Anonymous said...

cousin_itt, your cheap way to move the cursor to the end also works fine on Firefox. Thanks

Dinesh Gupta said...

how to do this in Firefox?
createTextRange is for IE