• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 741
  • Last Modified:

insert special character at cursor point in a field

We need to insert special characters like

±
µ
º

into a form field.

What we need is script that insert special characters (like: ± µ º and others) after the cursor in a form field.

Like this:

form action=2mypage.htm" name="myForm" action="post">
textarea cols="12" rows="8" name="specs">/textarea>

input type="button" value="insert ±" onClick="insertChar('specs','±'">

input type="button" value="insert micro" onClick="insertChar('specs','µ'">

/form>

We "only" need the script insertChar...

Thank you in advance
Joergen
0
jastroem
Asked:
jastroem
  • 4
  • 4
1 Solution
 
tejaspuraniCommented:
this works fine in IE 6 not good in NS 6.2.2

T.P.

<HTML>
<HEAD>

<script language="javascript">
function test(item,text)
{
     item.innerText= item.value + text ;
}
</script>
</HEAD>

<BODY>
<form name=frm>
<input type=text name=txt1>
<br>
<textarea name=txtA  cols=20 rows=5  ></textarea>
<br>
<input type=button name=btn1 onclick="test(txtA,'&micro;')">
<br>
<input type=button name=btn1 onclick="test(txtA,'&plusmn;')">

<br>
&plusmn;
<br>
&micro;
</form>
</BODY>
</HTML>
0
 
jastroemAuthor Commented:
tejaspurani

thank you for your comment!

The script works so far good, though I hav the following questions:

- the character is inserted at the end of the <textarea>, at the cursor point would have been the best

- what about downwards compability to IE 5 or 5.5?

Since this is for a web admin interface compability is not that important. But if it would work with IE 5 it would offcourse be great.

/ joergen
0
 
tejaspuraniCommented:
Hi ,

I don't have ie 5.5
so i can not test it.

but works fine in ie 4 and 5.

T.P.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jastroemAuthor Commented:
Ok - thanks!

But what about inserting at the point where the cursor is positioned - do think that can be realized?

/ joergen
0
 
tejaspuraniCommented:
<script language="javascript">
function test(item,text)
{
    item.focus();//didn't tested but this will insert
    //the char at the end of text
    item.innerText= item.value + text ;
}
</script>

T.P.
0
 
jastroemAuthor Commented:
i'm sorry but this does the same as before:
it inserts the text at the end of the text.

What i'm looking for is something that insert the text at the place where the cursor is - this can also be somewhere in the middle of a text.

/ joergen
0
 
tejaspuraniCommented:
Try This
T.P.

<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="content" COLS=40 ROWS=6
 ONSELECT="storeCaret(this);"
 ONCLICK="storeCaret(this);"
 ONKEYUP="storeCaret(this);">Some example text</textarea>
<br>
<INPUT TYPE="button" STYLE="font-family:courier;" VALUE="-COOL-"
 ONCLICK="insertAtCaret(this.form.content,'-COOL-');">
 <br>
<input type=button name=btn1 value='&micro;' onclick="insertAtCaret(this.form.content,'&micro;')">
<br>
<input type=button name=btn1 value='&plusmn;' onclick="insertAtCaret(this.form.content,'&plusmn;')">
<br>

</FORM>
</BODY>
</HTML>
0
 
jastroemAuthor Commented:
Hi again

That's really great!

Thanks a lot / Joergen
0
 
contiwCommented:
Greetings Tejaspurani and All

Tried to InsertCaretAt(...
from a child window
and made a fine mess.
Could you help?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now