Welcome to WebmasterWorld Guest from 50.17.5.36

Forum Moderators: open

Message Too Old, No Replies

Cursor position in TEXTAREA field

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

New User

10+ Year Member

joined:July 27, 2003
posts:37
votes: 0


Hello!

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!

Steve

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 22, 2002
posts:1749
votes: 0


Will a simple focus() be enough?

document.nameOfMyForm.nameOfInputBox.focus();

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

New User

10+ Year Member

joined:July 27, 2003
posts:37
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 22, 2002
posts:1749
votes: 0


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.focus();
inputEl.setSelectionRange(selStart, selEnd);
} else if (inputEl.createTextRange) {
var range = inputEl.createTextRange();
range.collapse(true);
range.moveEnd('character', selEnd);
range.moveStart('character', selStart);
range.select();
}
}

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)

New User

10+ Year Member

joined:July 27, 2003
posts:37
votes: 0


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

Steve

kedsri

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

Inactive Member
Account Expired

 
 


Mr.RonPK

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.

Thanks,
Kedar