Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert text on textarea in cursor position

Status
Not open for further replies.

theScien

Technical User
Aug 27, 2003
98
PT
Hi all,
I have a little problem that I hope some of you might be able to solve.

I want to add a predefined piece of text to a textarea but where the blinking cursor is, and not by the end of it.

I have the following:

<IMG SRC=&quot;grfx/smilie_01.gif&quot; ALIGN=&quot;absmiddle&quot; onClick=&quot;addSMILIE('[:)]')&quot; STYLE=&quot;cursor: hand;&quot;>

This adds the [:)] to the textarea fine, but always by the end, and not where the input cursor is, here's the function I'm using:

function addSMILIE(code) {
document.frmPROPA.txtNEWS.focus();
document.frmPROPA.txtNEWS.value += (code);
}

How to insert the code to the textarea but where the input blinking cursor is and not by the end of it?

Thanks.

If you haven't heard of it, then you most likely don't need it.
---------------------------------------------------------------------
---------------------------------------------------------------------
 
You can use clipboard commands but I don't remmember if they work in Netscape. You can place text in the clipboard like this:

Code:
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function ClipBoard(txt) 
{
holdtext.innerText = txt;
Copied = holdtext.createTextRange();
Copied.execCommand(&quot;Copy&quot;);
}
</SCRIPT>

I cut the above code out of a working app... I think it needs some form variables named holdtext and copied...

then put it in the textarea where you want it like this:

Textareaname.execCommand(&quot;Paste&quot;)

I hope that helps.


Travis Hawkins
BeachBum Software
travis@cfm2asp.com
 
Thx, already found a solution, here it is for anyone who might need it:

<html>
<head>

<script type=&quot;text/javascript&quot;>
function storeCaret(ftext)
{
if (ftext.createTextRange)
{
ftext.caretPos = document.selection.createRange().duplicate();
}
}

function insertsmilie(smilieface)
{
var t = document.f.t;
if (t.createTextRange && t.caretPos)
{
var caretPos = t.caretPos;
caretPos.text = smilieface;
t.focus();
}
else
{
t.value+=smilieface;
t.focus();
}
}
</script>

<body>
<form name=f>
<textarea name=t onselect=&quot;storeCaret(this);&quot; onclick=&quot;storeCaret(this);&quot; onkeyup=&quot;storeCaret(this);&quot; onchange=&quot;storeCaret(this);&quot;>Text goes here</textarea><br>
<button onclick=&quot;insertsmilie('8)');&quot;>Press</button><br>
</form>
</body>
</html>

If you haven't heard of it, then you most likely don't need it.
---------------------------------------------------------------------
---------------------------------------------------------------------
 
I'd like to get the start, end, and/or current position of the caret. Is this possible?

Thanks,

________________________
JoelMac
 
@ theScien:

Nice code. Would it be fine if I used it for a personal site? I've modified it just a little:

<button name=b onclick=&quot;insertsmilie(document.f.b.value);&quot;>Press</button><br>

So it puts whatever text is written on the button in the textbox.
 
Joelmac: I think you need to use the selection object, have a look at this: and this:
JESTAR: You can use it in any way you want.

---------------------------------------------------------------------
If you haven't heard of it, then you most likely don't need it.
---------------------------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top