[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

insert special character at cursor point in a field

Posted on 2002-05-16
9
Medium Priority
?
738 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
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…
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…

649 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