简体   繁体   中英

Paste selected text to another textarea

is it possible with pure JS to paste selected text into specified textarea? In my case, I want to select text in one of the textareas and when ctrl & A is pressed, selected text would paste into last (V1) textarea.

I have found similar case ( https://jsfiddle.net/QenBV/1/ ), but it's only made for 1 input textarea, but I have large number of textareas.

 function Addkey(e) { if (e.ctrlKey && e.keyCode == 65) { e.preventDefault(); document.execCommand("copy"); } } document.addEventListener('keydown', Addkey, false);
 <textarea>Text1</textarea><br/> <textarea>Text2</textarea><br/> <textarea>Text3</textarea><br/> <p></p> <hr/> <p></p> <textarea id="V1"></textarea><br/>

As per your question, the following one is your first text area from where you want to copy text to others

<textarea rows="5" cols="80" id="input" onkeyup="copySelected(event)" >One two three</textarea>

And you want to paste the selected text to the following textarea's

<textarea rows="5" cols="80" id="selection" class="selection"></textarea>
<textarea rows="5" cols="80" id="selection1" class="selection"></textarea>
<textarea rows="5" cols="80" id="selection2" class="selection"></textarea>
<textarea rows="5" cols="80" id="selection3" class="selection"></textarea>
<textarea rows="5" cols="80" id="selection4" class="selection"></textarea>
<textarea rows="5" cols="80" id="selection5" class="selection"></textarea>
<textarea rows="5" cols="80" id="selection6" class="selection"></textarea>

This would be your pure javascript code.

<script>
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(event) {

    if(event.ctrlKey && ())
    var srcTextarea = document.getElementById("input");
    var destTextarea = document.getElementsByClassName("selection");
    var txtd=getSelectedText(srcTextarea);
    <!-- if u want to paste the text only last one of your textarea -->
    
    destTextarea[destTextarea.length-1].value = txtd;
    
    <!-- if u want to paste the text all of the textarea then use following code block -->
    /*
     for(i=0; i<destTextarea.length; i++){
      destTextarea[i].value = txtd;
     }
    */

}

</script>

Here, when you press "ctrl + a" in your first textarea, then the text will be paste into all other textarea.

You can simply find out the focused text field using javascript/jquery and then copy the text when selected and concatenate it to the existing text in V1 text area Or you can simply add a button at the end of the text fields and when the button is clicked you can find texts of every text field using javascript/jquery and concatenate each text and paste into v1 text area

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM