Solved

insert special character at cursor point in a field

Posted on 2002-05-16
9
735 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses how to create an extensible mechanism for linked drop downs.
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)
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

690 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