最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论