# Insert string into textfield at cursor position

**URL:** <https://forum.kirupa.com/t/insert-string-into-textfield-at-cursor-position/634690>\
**Category:** flash\
**Created:** [May 28, 2016, 10:15pm UTC](https://forum.kirupa.com/t/insert-string-into-textfield-at-cursor-position/634690 "2016-05-28T22:15:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![McLucky](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@McLucky](https://forum.kirupa.com/u/McLucky)\
**Post date:** [May 28, 2016, 10:15pm UTC](https://forum.kirupa.com/t/insert-string-into-textfield-at-cursor-position/634690/1 "2016-05-28T22:15:20Z")

</div>

Hi,

What I’m trying to do is: Inserting a text (string) into a textfield by pasting.

- User puts the cursor at a position
- User clicks the 'Paste" button
- A string then has to be inserted at the cursor position, keeping all the text that is already there.

![](http://s33.postimg.org/d3n084ie7/interface.jpg)

Code below should do the magic, but how to paste at the cursor position ?  
Any hints would be welcome !

[code]btnPaste.addEventListener(MouseEvent.CLICK, fncInsert);

function fncInsert(e:Event):void{  
Clipboard.generalClipboard.clear();  
System.setClipboard(“String to insert”);

```
// PASTE STRING AT CURSOR POSITION IN TEXTFIELD
// WHILE KEEPING ALL EXISTING TEXT IN TEXTFIELD

```

}[/code]

Thanks in advance !

---

<div class="post-metadata">

**Author:** ![linusj](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@linusj](https://forum.kirupa.com/u/linusj)\
**Post date:** [May 29, 2016, 8:15am UTC](https://forum.kirupa.com/t/insert-string-into-textfield-at-cursor-position/634690/2 "2016-05-29T08:15:45Z")

</div>

If this is As3 and a TLFTextfield, you could probably use caret?  
[http://help.adobe.com/en\_US/FlashPlatform/reference/actionscript/3/fl/text/TLFTextField.html#caretIndex](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/text/TLFTextField.html#caretIndex)

Say, is this for school and you go to a forum to get the code?

---

<div class="post-metadata">

**Author:** ![McLucky](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@McLucky](https://forum.kirupa.com/u/McLucky)\
**Post date:** [May 29, 2016, 9:52am UTC](https://forum.kirupa.com/t/insert-string-into-textfield-at-cursor-position/634690/3 "2016-05-29T09:52:13Z")

</div>

No it’s not a school assignement 🙂  
It’s a personal project and I was hoping someone could give me a hint on how to do this the quick way. If I start without a starting point I’ll probably end up with way too complicated code

And i’ts a classic input textfield

---

<div class="post-metadata">

**Author:** ![McLucky](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@McLucky](https://forum.kirupa.com/u/McLucky)\
**Post date:** [May 29, 2016, 12:12pm UTC](https://forum.kirupa.com/t/insert-string-into-textfield-at-cursor-position/634690/4 "2016-05-29T12:12:10Z")

</div>

I ended up with this:

[code]  
function fncInsertStandardData(e:Event):void{  
var strComplete:String = txtOverview.text;  
var strA:String = strComplete.substr(0, txtOverview.selectionBeginIndex);  
var strB:String = strComplete.substr(txtOverview.selectionEndIndex, strComplete.length);

```
var strInsert:String = "";
strInsert = e.target.selectedItem.label + "\n" + e.target.selectedItem.data + "\n";

txtOverview.text = strA + strInsert + strB;
cboTypeInsert.selectedIndex = 0;

```

}[/code]

---

<div class="post-metadata">

**Author:** ![linusj](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@linusj](https://forum.kirupa.com/u/linusj)\
**Post date:** [May 30, 2016, 6:14am UTC](https://forum.kirupa.com/t/insert-string-into-textfield-at-cursor-position/634690/5 "2016-05-30T06:14:55Z")

</div>

+1 for your clean presentation. There is a built in replaceText function, but it won’t help you much.

[http://help.adobe.com/en\_US/FlashPlatform/reference/actionscript/3/flash/text/engine/TextElement.html#replaceText()](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/engine/TextElement.html#replaceText())
