Paste the selected text into the page in textarea (jQuery)

You must paste the selected text into the page in the text box. There must be a button for this.

+2
source share
2 answers
jQuery(function() {
    // Bind the click handler of some button on your page
    jQuery('#someButton').click(function(evt) {
        // Insert the selected text into a given textarea
        var textarea = jQuery('textarea#someTextArea');
        textarea.val(textarea.val() + getSelectedText());
        evt.preventDefault();
    });
});

// Get user selection text on page
function getSelectedText() {
    if (window.getSelection) {
        return window.getSelection();
    }
    else if (document.selection) {
        return document.selection.createRange().text;
    }
    return '';
}
+11
source

You can do something like this:

  • Copy selected text that you can use. Some jquery plugins are listed here .
  • Paste it inside the text box:

    $ ('# textareaselector'). Text (selectedText)

+2
source

Source: https://habr.com/ru/post/1771029/


All Articles