javascript - In ExtJs, how to insert a fixed string at caret position in a TextArea? - Stack Overflow

This question probably is asked in other framework, not sure if there is one on ExtJs, which I am new t

This question probably is asked in other framework, not sure if there is one on ExtJs, which I am new to. I wonder whether there is a simple example with a TextArea and a button. When the button is pressed, a fixed string "???" is inserted at the cursor in the TextArea.

Thanks in advance.

This question probably is asked in other framework, not sure if there is one on ExtJs, which I am new to. I wonder whether there is a simple example with a TextArea and a button. When the button is pressed, a fixed string "???" is inserted at the cursor in the TextArea.

Thanks in advance.

Share Improve this question edited Dec 8, 2012 at 3:31 dda 6,2132 gold badges27 silver badges35 bronze badges asked Dec 8, 2012 at 3:27 pktCoderpktCoder 1,1152 gold badges16 silver badges32 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You can actually do this straight from the DOM, using textareas selectionStart attribute to find the caret position.

So you could do something along the lines of

textArea.value = textArea.value.substring(0, selectionStart)+'???'+textArea.value.substring(selectionStart);

Here is a jsfiddle demonstrating this using a bination of Ext.get and Ext.getDom to select and modify the elements.

In extjs 7.5.1 (maybe older), you can get the cursor position with the getCaretPos() method of the textArea object :

    var textToInsert = 'test';
    var txtArea = Ext.ComponentQuery.query('#yourTextAreaItemId')[0];
    var currentContent = txtArea.getValue();
    var caretPos = txtArea.getCaretPos();

    txtArea.setValue(currentContent.substring(0,caretPos) + textToInsert + currentContent.substring(caretPos));

Beware that this method does not seem to be documented in the framework docs so beware of possible updates.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745312122a4622046.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信