2

Need to insert selected text on the page into textarea. There must be some button to do it.

Mike
  • 1,757
  • 3
  • 20
  • 26

2 Answers2

11
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 '';
}
Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
  • this works, but if textarea already have some text, the function removes it with a selected text. How can I fix this? – Mike May 02 '09 at 17:27
  • I mean it must "to add" text info textarea, not "to replace". – Mike May 02 '09 at 17:29
  • >it must add text into textarea without replacing – Mike May 02 '09 at 17:33
  • I modified my post accordingly – Darin Dimitrov May 02 '09 at 17:34
  • is it possible to insert not a simple text, with some tag within? For example, we are selecting word "Darin", pushing the button "#someButton" and it adds the "Darin" into textarea with a tag "[q]" -"[q]Darin[/q]"? – Mike May 02 '09 at 17:36
  • I am not sure if it is possible to get HTML tags that wrap user selection in a browser. – Darin Dimitrov May 02 '09 at 17:42
  • >I modified my post accordingly still not works, it removes all the text in textarea when inserting, must add a new to the end. – Mike May 02 '09 at 17:44
  • >that wrap user selection in a browser emm, I hope you dont understand me. It must add some text and NOT JUST the text, some extra text with it, which will be in code of our function. I mean not "getSelectedText()", "'some text'+getSelectedText()+'some text'". – Mike May 02 '09 at 17:48
  • can it remove all the tabs in selected text before insert? – Mike May 02 '09 at 17:50
  • >It must add some text and NOT JUST the text, // I have done it - textarea.val(textarea.val() + '[quote]' + getSelectedText() + '[/quote]'); // what about other? – Mike May 02 '09 at 17:53
  • How about you try to do it on your own and not have other people do the work for you? He's already gotten you where you are, all the things you are asking are simple if you put forth the effort. Don't be a leech. – Paolo Bergantino May 02 '09 at 18:09
  • >it must to remove all the tabs in selected text before insert - thats the last question to answer – Mike May 02 '09 at 18:12
  • Paolo Bergantino, its a simple maybe, but I cant do it myself, thats why I'm asking for it. I'm good in html and design, but not javascript, began to learn it few days ago. Man is answered, I gave a plus for his post, is it bad? – Mike May 02 '09 at 18:16
2

You can do something like this:

  • Copy the selected text you can use some jquery plugin listed here.
  • Paste it inside a textarea:

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

Community
  • 1
  • 1
eKek0
  • 21,835
  • 24
  • 87
  • 117
  • this page doesnt work - http://laboratorium.0xab.cd/jquery/fieldselection/0.2.3-test/test.html – Mike May 02 '09 at 17:22