text is not selected in js tried all methods in. select() and btn.select() in not selecting the text in textbox
var in = document.getElementById("in");
var btn = document.getElementById("btn");
btn.onclick() = function(){
in.focus();
in.select();
in.setSelectionRange(0,9999);
document.execCommand("copy");
}; <body>
<input id="in" type="text">
<button id="btn" >Copy </button>
</body>Advertisement
Answer
in is a reserved key in javascript so cant use it and you have to remove () after the onclick when you want to assign it to value.
var inputField = document.getElementById("in");
var btn = document.getElementById("btn");
btn.onclick = function(){
inputField.focus();
inputField.select();
inputField.setSelectionRange(0, 99999)
document.execCommand("copy");
};
var linkTag = document.getElementById("link");
var btnLink = document.getElementById("btn-link");
btnLink .onclick = function(){
const range = document.createRange();
range.selectNode(linkTag );
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
}; <body>
<div>
<input id="in" type="text">
<button id="btn" >Copy </button>
</div>
<div>
<a href="google.com" id="link">google.com</a>
<button id="btn-link" >Copy </button>
</div>
</body>