EN
JavaScript - select all input text on focus
4 points
In this short, article we would like to show how to select all text on focus in input element using JavaScript.
Quick solution:
xxxxxxxxxx
1
<input type="text" value="Some text here ..." onfocus="this.select()" onmouseup="return false" />
xxxxxxxxxx
1
2
<html>
3
<body>
4
<input id="text-input" type="text" value="Some text here ..." />
5
<script>
6
7
var element = document.querySelector('#text-input');
8
9
element.addEventListener('focus', function(e) {
10
element.select();
11
});
12
13
element.addEventListener('mouseup', function(e) {
14
e.preventDefault();
15
});
16
17
</script>
18
</body>
19
</html>