This code should select the contents of a textarea when that textarea is selected. However, it doesn't work by itself:
$("#form").focus(function() {
$("#form").select();
});
It only works when I give it a setTimeout of 0, like so:
$("#form").focus(function() {
setTimeout(function() {
$("#form").select();
}, 0);
});
Why is that?
It looks like the first code snippet is selecting, then deselecting the text. I suspect that the text selection is being handled behind the scenes after the focus event is finished, and setTimeout executes the select after the event (when the behind-the-scenes stuff has already happened).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With