Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Cursor position in TEXTAREA field



4:24 pm on Oct 8, 2005 (gmt 0)

10+ Year Member


I'm wanting a user's cursor to go to the start of a text entry field when it's selected. It's for a message reply form and when the page loads I would like the cursor to be in the text box above the quoted text. Any ideas?

Thanks folks!



8:08 pm on Oct 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Will a simple focus() be enough?



8:10 am on Oct 10, 2005 (gmt 0)

10+ Year Member

Thanks for the reply. I currently us onFocus() but I need to place the user's cursor before the entered text. For example, consider the following being the contents of an HTML textarea:

---- Original Message ----
Hello. This is my message.

By using onFocus() the user's cursor would be placed at the end of the text, whereas they would want their reply to appear before the top of the original message.


9:19 am on Oct 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Ah, I see. Here's a small function to manipulate the caret position. It actually creates a selection if selStart and selEnd are not equal.

function setSelRange(inputEl, selStart, selEnd) { 
if (inputEl.setSelectionRange) {
inputEl.setSelectionRange(selStart, selEnd);
} else if (inputEl.createTextRange) {
var range = inputEl.createTextRange();
range.moveEnd('character', selEnd);
range.moveStart('character', selStart);

Call the function with setSelRange(document.formName.replyHere, 0, 0) to set the caret at the start of the textarea called 'replyHere' (or input element).


10:19 am on Oct 10, 2005 (gmt 0)

10+ Year Member

Fantastic! Thanks a lot - it works perfect! :)



6:49 pm on Oct 14, 2005 (gmt 0)


I have a Textarea and in that Text area i am selecting values from a javascript child window. let say the Parent window as par.html and child window as child.html.

From the child.html i send "ABC" values as
opener.parent.par.txtBox.value. so this value ABC goes into the par.html text area as ABC. Now if i want ABXYZC in my text area. so i have to place my cursor in between AB and the values XYZ from the child window shd be inserted as ABXYZC.

how to achieve this? your help is highly appreciated.



Featured Threads

Hot Threads This Week

Hot Threads This Month