EN
JavaScript - select all text inside input
3 points
In this article, we would like to show you how select all text inside <input>
or <textarea>
element using JavaScript.
Quick solution:
xxxxxxxxxx
1
const element = document.querySelector('#text-input');
2
3
element.select();
xxxxxxxxxx
1
2
<html>
3
<body>
4
<input type="text" id="text-input" value="Dirask is awesome!" />
5
<button onclick="selectText()">Select</button>
6
<script>
7
8
var element = document.querySelector('#text-input');
9
10
function selectText() {
11
element.select();
12
}
13
14
</script>
15
</body>
16
</html>
Screenshots:

