var delayBecauseFirebase = 1000; setTimeout(function() { var buttonShowJ = document.getElementById("buttonShow"); var messagesInJ = document.getElementById("messagesIn"); if(buttonShowJ) { buttonShowJ.addEventListener("click",function(){ var info = messagesInJ.innerHTML.replace(`<button id="buttonShow">Copy</button>`,""); info.select(); \**(!HERE, because that doesnt works)** document.execCommand("copy"); }); }else{ console.log("error"); } }, delayBecauseFirebase);
I want to select the “text” inside info to can do
document.execCommand(“copy”);
But i dont know how can i use select for that var.
Advertisement
Answer
I think this could work
- In the HTML
<div class="container"> <div id="messagesIn"> ...Some Text </div> <button id="buttonShow">Copy</button> </div>
- Then in the code
var delayBecauseFirebase = 1000; function copyText(){ //Select your text var range = document.createRange(); range.selectNode(document.getElementById("messagesIn")); window.getSelection().removeAllRanges(); window.getSelection().addRange(range); //Call copy command document.execCommand("copy"); } function addClickListener() { var buttonShowJ = document.getElementById("buttonShow"); if(buttonShowJ){ buttonShowJ.addEventListener("click", copyText); }else{ console.error("Copy button not found"); } } setTimeout(addClickListener, delayBecauseFirebase);
Recomended post: