I wrote this code to copy multiple textareas’ content with a unique button, but only the first textarea is copied.
var copyText = document.getElementsByClassName("form-control")[0];
function myFunction() {
// Get the text field
// Select the text field
copyText.select();
copyText.setSelectionRange(0, 99999); // For mobile devices
// Copy the text inside the text field
document.execCommand("copy");
// Alert the copied text
alert("Copied the text: " + copyText.value);
///second
}
<textarea class="form-control" tabindex="0">something</textarea>
<button onclick="myFunction()">clickme</button>
<textarea class="form-control"tabindex="3" >something1</textarea>
<button onclick="myFunction()">clickme</button>
<textarea class="form-control" >something2</textarea>
<button onclick="myFunction()">clickme</button>
How can I resolve this issue so that multiple textareas are copied, instead?
2
Answers
To duplicate the text, from text boxes using one button you must iterate through all the text boxes and replicate their content. Here is how you can adjust your JavaScript function to accomplish this;
I strongly suggest you delegate. Right now you only select the very first field on purpose
Here any click on a button with class="copy" in the container div will copy the textarea before it