Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • 1
    This looks to be an elegant solution. The complete code would look like: <input type="text" onclick="document.execCommand('selectall',null,false);" /> Commented Dec 26, 2016 at 10:07
  • Yeah, it's my favourite way to do this kind of task, esp. as it works for contenteditable elements as well. I think you can also make it even slightly more elegant, i.e. <input type="text" onfocus="document.execCommand('selectall')"> - pretty sure you can remove the null and false if you don't use them. Commented Dec 28, 2016 at 3:16
  • 2
    when using onfocus, the whole page is selected when input is clicked in Chrome. onclick works fine. Commented May 16, 2017 at 23:18
  • 1
    MDN says execCommand is now deprecated. Commented Jun 8, 2021 at 3:58
  • 1
    @arcanemachine Cheers, thanks for letting me know. I've added a notice to the post :) Commented Jun 8, 2021 at 4:06