tags:

views:

4512

answers:

2

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

+6  A: 
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
this works, but if textarea already have some text, the function removes it with a selected text. How can I fix this?
Mike
I mean it must "to add" text info textarea, not "to replace".
Mike
>it must add text into textarea without replacing
Mike
I modified my post accordingly
Darin Dimitrov
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
I am not sure if it is possible to get HTML tags that wrap user selection in a browser.
Darin Dimitrov
>I modified my post accordinglystill not works, it removes all the text in textarea when inserting, must add a new to the end.
Mike
>that wrap user selection in a browseremm, 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
>I modified my post accordingly // sorry, works now :)
Mike
can it remove all the tabs in selected text before insert?
Mike
and keep a new lines
Mike
>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
dont sleep please (
Mike
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
>it must to remove all the tabs in selected text before insert - thats the last question to answer
Mike
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
the green is better :)
Mike
darin, will you fix it??
Mike
anyway, thanks for post
Mike
+2  A: 

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)

eKek0
this page doesnt work - http://laboratorium.0xab.cd/jquery/fieldselection/0.2.3-test/test.html
Mike
corrected the url. Check again please
eKek0
thanks, but I dont know how to use it :)
Mike