473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Insert text at current cursor position in TextArea

balabaster
797 Expert 512MB
How do I insert text into a textarea at the current cursor position in JavaScript?
Oct 7 '08 #1
12 19173
pronerd
392 Expert 256MB
I do not think you can do that. I do not know of any events that will give you the position of the cursor/carrot in a text field. There is a way to get selected text, and then replace, modify, remove that text.
Oct 7 '08 #2
rnd me
427 Expert 256MB
javascript cannot do it?

ha, that's a new one...

for FF:

Expand|Select|Wrap|Line Numbers
  1. function insertText(textBox, strNewText){
  2.   var tb = textBox;
  3.   var first = tb.value.slice(0, tb.selectionStart);
  4.   var second = tb.value.slice(tb.selectionStart);
  5.   tb.value = first + strNewText + second;
  6. }
an ie version is far more complicated, do you need it?
Oct 7 '08 #3
balabaster
797 Expert 512MB
javascript cannot do it?

ha, that's a new one...

for FF:

Expand|Select|Wrap|Line Numbers
  1. function insertText(textBox, strNewText){
  2.   var tb = textBox;
  3.   var first = tb.value.slice(0, tb.selectionStart);
  4.   var second = tb.value.slice(tb.selectionStart);
  5.   tb.value = first + strNewText + second;
  6. }
an ie version is far more complicated, do you need it?
I've got something that kind-of works in IE. It's a little buggy, but I haven't had time to work out the bugs yet. If you've got something that works in IE, I'd be happy to see it.
Oct 8 '08 #4
rnd me
427 Expert 256MB
I've got something that kind-of works in IE. It's a little buggy, but I haven't had time to work out the bugs yet. If you've got something that works in IE, I'd be happy to see it.
well i threw the firefox code together off the top of my head.

turns out i guess i don't have an ie version of this.
i have code that happily wraps (fore example) "<b>xxx</b>" around selected text in a textarea, but if there is no selection at all, it doesn't work.

one crappy workaround i thought up would be to use execCommand("paste") to insert the clipboard into the text. haven't tested this on textareas though.

you could use another function to gain raw access to the clipboard from javascript:


Expand|Select|Wrap|Line Numbers
  1. function CB() //sets or read clipboard in IE accepts 1 arg to set, returns cb when empty
  2. {
  3. if (arguments.length)     {window.clipboardData.setData('Text', arguments[0])}
  4. else     { return window.clipboardData.getData('Text')}
  5.  }//end CB
using it, you could then
1. backup the current clipboard text to a var
2. set the clipboard to the string to be inserted.
3. execCommand("paste")
4. set the clipboard to the var from step 1.

this could ruin the user's clipboard if he has for instance, and image or file on the clipboard, but would be ok if he had just plain text.
Oct 8 '08 #5
balabaster
797 Expert 512MB
well i threw the firefox code together off the top of my head.

turns out i guess i don't have an ie version of this.
i have code that happily wraps (fore example) "<b>xxx</b>" around selected text in a textarea, but if there is no selection at all, it doesn't work.

one crappy workaround i thought up would be to use execCommand("paste") to insert the clipboard into the text. haven't tested this on textareas though.

you could use another function to gain raw access to the clipboard from javascript:


Expand|Select|Wrap|Line Numbers
  1. function CB() //sets or read clipboard in IE accepts 1 arg to set, returns cb when empty
  2. {
  3. if (arguments.length)     {window.clipboardData.setData('Text', arguments[0])}
  4. else     { return window.clipboardData.getData('Text')}
  5.  }//end CB
using it, you could then
1. backup the current clipboard text to a var
2. set the clipboard to the string to be inserted.
3. execCommand("paste")
4. set the clipboard to the var from step 1.

this could ruin the user's clipboard if he has for instance, and image or file on the clipboard, but would be ok if he had just plain text.
In addition, the user would be prompted with an annoying message asking them if the application can have access to their clipboard... so that rules that option out.
Oct 9 '08 #6
rnd me
427 Expert 256MB
In addition, the user would be prompted with an annoying message asking them if the application can have access to their clipboard... so that rules that option out.
bummer.
it sucks that IE has this limitation.

just one more thought:

IE supports VBscript.
VBscript has a sendkeys method.
perhaps you could send a weird char or two to the input, like "^~^" at the cursor.
you could then run a simple replace on the whole value to "insert" the text.

i don't know enough about vbscript to throw together some code, but you should be able to use both languages on the same page. you might need to staple it together using a hidden input.
and easy way to talk between js and vb is to use input.value (easily accessible from both) to pass a string. you can bind both vb and js event to the same input.
each half of the code should only be a couple lines.

just an idea, not sure if it's a good one.
Oct 9 '08 #7
balabaster
797 Expert 512MB
IE supports VBscript.
VBscript has a sendkeys method.
perhaps you could send a weird char or two to the input, like "^~^" at the cursor.
you could then run a simple replace on the whole value to "insert" the text.
Interesting thought... I'll have a play and see what I can come up with.
Oct 9 '08 #8
acoder
16,027 Expert Mod 8TB
Is this any help?
Oct 9 '08 #9
Frinavale
9,735 Expert Mod 8TB
This thread might help too...

-Frinny
Oct 9 '08 #10
rnd me
427 Expert 256MB
hooray!
thanks acoder. the article was kinda overkill, but a comment posted on the page revealed the gem below.

this seems to work as long as textarea is in focus:

Expand|Select|Wrap|Line Numbers
  1. var text = "hello world"
  2. document.selection.createRange().text=text;
if used from a button, clicking the button will blur the textarea.
give it a textAreaObject.focus() right before to be safe.
Oct 9 '08 #11
balabaster
797 Expert 512MB
hooray!
thanks acoder. the article was kinda overkill, but a comment posted on the page revealed the gem below.

this seems to work as long as textarea is in focus:

Expand|Select|Wrap|Line Numbers
  1. var text = "hello world"
  2. document.selection.createRange().text=text;
if used from a button, clicking the button will blur the textarea.
give it a textAreaObject.focus() right before to be safe.
Thanks guys, that's kind of what I have, it's just a little buggy...
Oct 9 '08 #12
acoder
16,027 Expert Mod 8TB
What problems are you having with it?

Have you tried the link in the thread posted by Frinavale?
Oct 10 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: M Wells | last post by:
Hi All, I've been checking the online javascript libraries for code that will show me how to insert characters at the current cursor point in a textarea form field to help me markup content I...
2
by: Thomas | last post by:
Hi there, I'd like to insert a text into a textarea at the current cursor position when you click on a button, but I only found out how to insert a text at the end of the textarea. Is it...
1
by: Dave Hammond | last post by:
I have an html element (a link) with an onclick handler which opens a popup window, and would like to position that window at the coordinates where the element appears on the page. I have...
2
by: Matt Tapia | last post by:
Is there a way to insert text into a multi-row textbox where the cursor position is at in the multi-row textbox? Just wondering
1
by: Matt Tapia | last post by:
Is there a way to insert text into a multi-row textbox where the cursor position is at in the multi-row textbox? Just wondering..I was told I could use the SelectedText property however I get...
2
by: tony | last post by:
Hello! Assume I catch a double click in a form by using the event handler that forms designer create for me. Now to my question when I double click in the form the event handler is trigged...
1
by: b | last post by:
I've seen a lot of posts on this for textarea's and textboxes such as here: http://alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript but does anyone know how to insert text...
1
by: samsanjay | last post by:
Hi everyone, I m a beginner in C#. I m working to create an editor in C#. I want to display the current cursor position in statusbar.. How to find the richtextbox current cursor postion...?
3
by: ziycon | last post by:
I'm using the below function to add in a <br/> tag to the cusrors current position when the button is clicked, its adding in the <br/> at the begining everytime and not where the cursor is, I'm...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.