text is not selected in js tried all methods in. select()
and btn.select()
in not selecting the text in textbox
JavaScript
x
9
1
var in = document.getElementById("in");
2
var btn = document.getElementById("btn");
3
4
btn.onclick() = function(){
5
in.focus();
6
in.select();
7
in.setSelectionRange(0,9999);
8
document.execCommand("copy");
9
};
JavaScript
1
4
1
<body>
2
<input id="in" type="text">
3
<button id="btn" >Copy </button>
4
</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.
JavaScript
1
23
23
1
var inputField = document.getElementById("in");
2
var btn = document.getElementById("btn");
3
4
btn.onclick = function(){
5
inputField.focus();
6
inputField.select();
7
8
inputField.setSelectionRange(0, 99999)
9
document.execCommand("copy");
10
};
11
12
var linkTag = document.getElementById("link");
13
var btnLink = document.getElementById("btn-link");
14
15
btnLink .onclick = function(){
16
const range = document.createRange();
17
range.selectNode(linkTag );
18
const selection = window.getSelection();
19
selection.removeAllRanges();
20
selection.addRange(range);
21
22
document.execCommand('copy');
23
};
JavaScript
1
10
10
1
<body>
2
<div>
3
<input id="in" type="text">
4
<button id="btn" >Copy </button>
5
</div>
6
<div>
7
<a href="google.com" id="link">google.com</a>
8
<button id="btn-link" >Copy </button>
9
</div>
10
</body>