4

I have two <textarea>s. One with id="input" and the other with id="selection".

<textarea id="input"> will contain some HTML. The user will select some text in this textarea, click a button and the selected text will be copied to <textarea id="selection">.

I can use jQuery or just vanilla JavaScript and I'd like it to work in IE7+, Safari and Firefox.

random
  • 9,774
  • 10
  • 66
  • 83
joeljoeljoel
  • 633
  • 1
  • 6
  • 8

3 Answers3

7

There's only one way I've been able to do it. The problem you're running into, as you may be aware, is that when you click the button (thus firing the event to copy the selection), the textarea loses focus, and thereby there's no text selected.

So as a workaround, I styled a div to look (kinda) like a textarea. That seems to work:

<style type="text/css">
    .textarea { 
        border:1px solid black; 
        width:200px; 
        height:100px; 
        overflow-y: auto; 
        float:left; 
    }
</style>

The markup then looks like this:

<div id="input" class="textarea">This is a test</div>
<textarea id="selection"></textarea>

<button id="theButton">Copy</button>

And finally, the script:

var selText = "";

$( document ).ready( function() {
    $( '#theButton' ).mousedown( function() {
        $( '#selection' ).val( getSelectedText() );
    });
});

function getSelectedText(){
    if ( window.getSelection ) {
        return window.getSelection().toString();
    }
    else if ( document.getSelection ) {
        return document.getSelection();
    } else if ( document.selection ) {
        return document.selection.createRange().text;
    }
} 

To give full credit where it's due, I got the getSelectedText() method from http://esbueno.noahstokes.com/post/92274686/highlight-selected-text-with-jquery

charliegriefer
  • 3,342
  • 1
  • 18
  • 20
  • How about saving the selected text to a variable on a `mouseleave` event on the ` – user113716 Dec 03 '10 at 04:11
  • I tried to grab the selected text on the blur event of the textarea, but that didn't work. Wasn't familiar with mouseleave. – charliegriefer Dec 03 '10 at 04:16
  • heh... my var selText = ""; was left there from my attempt to use the blur event. Forgot to remove it :) – charliegriefer Dec 03 '10 at 04:22
  • I did not realize that clicking the button would cause the selected text in the textarea to lose focus. This explains why I could get the selection to copy when binding the .select() event handler to #input but then it did not copy when binding the .click() event handler to #theButton. I think I will just get rid of the button and copy the text on select. However, I still cannot get it to work in Firefox 3.6.6. It does work in Safari though. I've a got an example on [jsfiddle](http://jsfiddle.net/joeljoeljoel/D7yxG/) – joeljoeljoel Dec 03 '10 at 05:06
  • Yup... your jsfiddle page works for me in Chrome on OS X, but not Firefox. The select() event fires... I threw an `alert( 'foo' )` in there. But I can't `alert( getSelectedText() )`. Bet the textarea is still throwing it off somehow :( – charliegriefer Dec 03 '10 at 05:16
  • To clarify, when I try `alert( getSelectedText() )`, I get an empty alert. – charliegriefer Dec 03 '10 at 05:16
  • 1
    This is all completely unnecessary because it actually works fine with a `click` event on a button. See my answer. – Tim Down Dec 03 '10 at 12:28
  • Yup. Just tried @Tim's jsFiddle example on Firefox and it works perfectly. So, I'm new here... do I downvote my own answer? :) – charliegriefer Dec 03 '10 at 17:03
  • @charliegriefer: No. Your answer isn't exactly wrong, so I'd leave it there and be happy with the five votes :) Alternatively, if you deleted it you'd get a badge ("Disciplined" - Deleted own post with score of 3 or higher) – Tim Down Dec 03 '10 at 17:20
6

The following will do it:

See it in action: http://www.jsfiddle.net/QenBV/1/

function getSelectedText(el) {
    if (typeof el.selectionStart == "number") {
        return el.value.slice(el.selectionStart, el.selectionEnd);
    } else if (typeof document.selection != "undefined") {
        var range = document.selection.createRange();
        if (range.parentElement() == el) {
            return range.text;
        }
    }
    return "";
}

function copySelected() {
    var srcTextarea = document.getElementById("input");
    var destTextarea = document.getElementById("selection");
    destTextarea.value = getSelectedText(srcTextarea);
}

<input type="button" onclick="copySelected()" value="copy selected">
Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • This works perfectly in Safari and Firefox. Do you know what other browsers this will work on on? It looks like the first if statement works for Safari and Firefox. Which browsers is the second if statement for? – joeljoeljoel Dec 03 '10 at 23:28
  • Works in IE 6 and later and Opera, so everything major. The second branch is for IE. – Tim Down Dec 03 '10 at 23:55
0

with jquery you can do as below

 $('#selection').val($('#input').val());

http://api.jquery.com/val/

kobe
  • 15,671
  • 15
  • 64
  • 91