Il semble que de plus en plus de navigateurs supportent setSelectionRange()
que select()
1 voie : - Utiliser setSelectionRange()
https://caniuse.com/#search=setSelectionRange
const my_textarea = document.getElementById("my_textarea");
document.getElementById("my_but").onclick = function () {
if(my_textarea.value !== ""){
my_textarea.onfocus = function () {
my_textarea.setSelectionRange(0, my_textarea.value.length);
my_textarea.onfocus = undefined;
}
my_textarea.focus();
}
}
<textarea id="my_textarea" name="text">1234567</textarea>
<br>
<button id="my_but">Select</button>
2 voies : - Utilisation select()
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select#browser_compatibility
const my_textarea = document.getElementById("my_textarea");
document.getElementById("my_but").onclick = function () {
if(my_textarea.value !== ""){
my_textarea.onfocus = function () {
my_textarea.select();
my_textarea.onfocus = undefined;
}
my_textarea.focus();
}
}
<textarea id="my_textarea" name="text">1234567</textarea>
<br>
<button id="my_but">Select</button>
0 votes
[2020] les navigateurs modernes ont
setSelectionRange
developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/