Inserting a text where cursor is using Javascript/jquery

前端 未结 13 2677
盖世英雄少女心
盖世英雄少女心 2020-11-22 03:58

I have a page with a lot of textboxes. When someone clicks a link, i want a word or two to be inserted where the cursor is, or appended to the textbox which has the focus.<

13条回答
  •  旧时难觅i
    2020-11-22 04:28

    using @quick_sliv answer:

    function insertAtCaret(el, text) {
        var caretPos = el.selectionStart;
        var textAreaTxt = el.value;
        el.value = textAreaTxt.substring(0, caretPos) + text + textAreaTxt.substring(caretPos);
    };
    

提交回复
热议问题