Insert text into flex textbox 3

I have a textArea and a list. When the user double-clicks a list item, the label of the selected item should be inserted into the text box. When text is selected in textArea, it must be replaced, otherwise the text simply needs to be pasted into existing text at the caret point.

I managed to get the text and that's it, I just can't seem to insert it at the caret point. Does anyone know how to do this?

+2


source to share


3 answers


It's not really JavaScript, it's Adobe Flex 3. Thanks for the help, although it got me headed in the right direction. This is done in Flex 3:



var caretStart:int = textArea.selectionBeginIndex;
var caretEnd:int = textArea.selectionEndIndex;
textArea.text = textArea.text.substring(0,caretStart)
              + newText
              + textArea.text.substr(caretEnd);

      

+2


source


The accepted answer works great if you don't have existing HTML formatting. In my case, I inserted a new button into the editor that the user could click to enter a keyword. I kept losing all HTML formatting until I dug into the actual class and sided with the TextRange object:

       public function keyWord_Click(event:Event) : void 
        {

            var caretStart:int = txtEditor.textArea.selectionBeginIndex;
            var caretEnd:int = txtEditor.textArea.selectionEndIndex;
            var newText : String = "[[[KEYWORD]]]";

            var tf:TextRange = new TextRange(txtEditor,true,caretStart,caretEnd);
            tf.text = newText;

        }

      



The nice thing about this approach is that you can also apply conditional formatting to this TextRange object as needed.

+1


source


You can use txtarea.selectionStart and txtarea.selectionEnd to get the selected position in the text.

After that, you delete the txt and add the new selected text.

I don't know much about Javascript, so I wrote it for U.

You can search on Google by keywords: "Javascript Selected Text TextArea" "Javascript adds text to position"

Example code: function insertAtCursor (myField, myValue) {// IE support if (document.selection) {myField.focus (); sel = document.selection.createRange (); sel.text = myValue; } // MOZILLA / NETSCAPE support else if (myField.selectionStart || myField.selectionStart == '0') {var startPos = myField.selectionStart; var endPos = myField.selectionEnd; myField.value = myField.value.substring (0, startPos) + myValue + myField.value.substring (endPos, myField.value.length); } else {myField.value + = myValue; }

caretPos = doGetCaretPosition(myField);
alert(caretPos);
setCaretPosition(myField,caretPos-3);

      

}

0


source







All Articles