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
2009-05-02 17:19:56
this works, but if textarea already have some text, the function removes it with a selected text. How can I fix this?
Mike
2009-05-02 17:27:06
I mean it must "to add" text info textarea, not "to replace".
Mike
2009-05-02 17:29:03
>it must add text into textarea without replacing
Mike
2009-05-02 17:33:34
I modified my post accordingly
Darin Dimitrov
2009-05-02 17:34:02
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
2009-05-02 17:36:56
I am not sure if it is possible to get HTML tags that wrap user selection in a browser.
Darin Dimitrov
2009-05-02 17:42:46
>I modified my post accordinglystill not works, it removes all the text in textarea when inserting, must add a new to the end.
Mike
2009-05-02 17:44:50
>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
2009-05-02 17:48:23
>I modified my post accordingly // sorry, works now :)
Mike
2009-05-02 17:49:24
can it remove all the tabs in selected text before insert?
Mike
2009-05-02 17:50:44
and keep a new lines
Mike
2009-05-02 17:51:03
>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
2009-05-02 17:53:00
dont sleep please (
Mike
2009-05-02 17:55:01
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
2009-05-02 18:09:02
>it must to remove all the tabs in selected text before insert - thats the last question to answer
Mike
2009-05-02 18:12:32
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
2009-05-02 18:16:25
the green is better :)
Mike
2009-05-02 18:16:45
darin, will you fix it??
Mike
2009-05-02 18:25:09
anyway, thanks for post
Mike
2009-05-02 19:57:24