Solved

insert special character at cursor point in a field

Posted on 2002-05-16
9
731 Views
Last Modified: 2013-12-03
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
Comment
Question by:jastroem
  • 4
  • 4
9 Comments
 
LVL 3

Expert Comment

by:tejaspurani
ID: 7014358
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
 

Author Comment

by:jastroem
ID: 7015623
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
 
LVL 3

Expert Comment

by:tejaspurani
ID: 7021832
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
 

Author Comment

by:jastroem
ID: 7022530
Ok - thanks!

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

/ joergen
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:tejaspurani
ID: 7022840
<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
 

Author Comment

by:jastroem
ID: 7023384
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
 
LVL 3

Accepted Solution

by:
tejaspurani earned 100 total points
ID: 7025082
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
 

Author Comment

by:jastroem
ID: 7025340
Hi again

That's really great!

Thanks a lot / Joergen
0
 

Expert Comment

by:contiw
ID: 7458078
Greetings Tejaspurani and All

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now