homepage Welcome to WebmasterWorld Guest from 54.166.255.168
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Cursor position in TEXTAREA field
YorkshireSteve

10+ Year Member



 
Msg#: 4527 posted 4:24 pm on Oct 8, 2005 (gmt 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

 

RonPK

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4527 posted 8:08 pm on Oct 8, 2005 (gmt 0)

Will a simple focus() be enough?

document.nameOfMyForm.nameOfInputBox.focus();

YorkshireSteve

10+ Year Member



 
Msg#: 4527 posted 8:10 am on Oct 10, 2005 (gmt 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.

RonPK

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4527 posted 9:19 am on Oct 10, 2005 (gmt 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).

YorkshireSteve

10+ Year Member



 
Msg#: 4527 posted 10:19 am on Oct 10, 2005 (gmt 0)

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

Steve

kedsri

10+ Year Member



 
Msg#: 4527 posted 6:49 pm on Oct 14, 2005 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved