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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting text at cursor position

Status
Not open for further replies.

remco5897

Technical User
Oct 27, 2001
48
NL
I am sure this is helpful to some of you. I was searching for something like this in TEK-TIPS and got the impression that it was impossible.

Try it out, its great!

Remco


I got the source from:

Author: VDSLUIS.NET
Language: JAVASCRIPT


<HTML>
<HEAD>
<TITLE>Example</TITLE>
<SCRIPT>
function storeCaret (textEl)
{
if (textEl.createTextRange) textEl.caretPos =
document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text)
{
if (textEl.createTextRange && textEl.caretPos)
{
var caretPos = textEl.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
}
else
textEl.value = text;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<TEXTAREA NAME=&quot;content&quot; COLS=40 ROWS=6
ONSELECT=&quot;storeCaret(this);&quot;
ONCLICK=&quot;storeCaret(this);&quot;
ONKEYUP=&quot;storeCaret(this);&quot;>Some example text</textarea>

<INPUT TYPE=&quot;button&quot; STYLE=&quot;font-family:courier;&quot; VALUE=&quot;-COOL-&quot;
ONCLICK=&quot;insertAtCaret(this.form.content,'-COOL-');&quot;>
</FORM>
</BODY>
</HTML>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top