Solved

insert special character at cursor point in a field

Posted on 2002-05-16
9
730 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
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…

706 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

12 Experts available now in Live!

Get 1:1 Help Now