Closed Thread Icon

Preserved Topic: Insert String at Cursor Pages that link to <a href="https://ozoneasylum.com/backlink?for=18565" title="Pages that link to Preserved Topic: Insert String at Cursor" rel="nofollow" >Preserved Topic: Insert String at Cursor\

 
Author Thread
Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 04-13-2002 08:30

Can anyone help me with a function that would enter a string of text in a textarea at the cursor position?


Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 04-13-2002 09:13

In IE, this should work:

var sel = document.selection.createRange(); // creates a "text range" object from the current selection
sel.text = "texttoinsert";

For more info, you'll have to look up the text range object... I really forget where I learned this.

Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 04-14-2002 17:51

That's kind of what I've got now, but you have to have something selected for it to work. Otherwise, it just prints the string at the top of the browser window.

I've been searching around, but I'm getting the impression that you can only detect a selection and not just the cursor position; is this true?


DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 04-14-2002 23:43

It should be possible, I want it myself, but this is a s close as I'm getting it:

code:
function insertSomething(thisField,theText){
document.all[thisField].insertAdjacentText("BeforeEnd", theText);
}


This places the text after all text in the textarea, but not at the cursor position...

This is the syntax I could find on it:
objectReference.insertAdjacentText(position, textstring)
or
objectReference.insertAdjacentHTML(position, textstring)

textstring is the text/HTML to be inserted and position is the where to insert, position has one of four values:
"BeforeBegin" (inserted immediately before the element.), "AfterBegin" (inserted after the element start tag but before any other enclosed content), "BeforeEnd" (before the element closing tag and after any other enclosed content) and "AfterEnd" (inserted immediately after the element ).

Now, why didn't they put "atCursor" in there, sigh...
/Dan


-{ a vibration is a movement that doesn't know which way to go }-

maninacan
Paranoid (IV) Inmate

From: Seattle, WA, USA
Insane since: Oct 2001

posted posted 04-15-2002 05:10

umm, maybe try this to get the cursor position:

IE:
xpos = window.event.clientX
ypos = window.event.clientY

NS:
xpos = evt.pageX
ypos = evt.pageY

and then if you know the textbox position just compare them, however that will only work if you know the textbox position. Maybe try onMouseover in the textbox, although that will probably not work, and you have probably already tried.
Good Luck.

http://www.kewlster.com

[This message has been edited by maninacan (edited 04-15-2002).]

Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 04-17-2002 07:42

That would probably work, DmS, but I think I like having the option to insert the text anywhere in my entry in case I want to go back and edit what I've written. I guess I'll have to stick with using a selection range. I'll just have to type a character, double click it, then click my button to replace it with the entire string.


Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 05-13-2002 05:08

Woohoo! After nearly a month, I seem to have found the answer. I slightly modified a snippet I found on YaBB:

code:
function insertText(text) {
if (document.formname.textareaname.createTextRange && document.formname.textareaname.caretPos) {
var caretPos = document.formname.textareaname.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
text + ' ' : text;
document.formname.textareaname.focus();
}
else document.formname.textareaname.value += text;
document.formname.textareaname.focus();
}

function snippetA() {
AddTxt='Snippet A';
insertText(AddTxt);
}

function snippetB() {
AddTxt='Snippet B';
insertText(AddTxt);
}



I even added the focus bit to bring the cursor back to the textarea so I could keep typing without clicking back inside it.

Hope someone finds this useful!

« BackwardsOnwards »

Show Forum Drop Down Menu