Languages
[Edit]
EN

JavaScript - select all text inside input

3 points
Created by:
Adnaan-Robin
484

In this article, we would like to show you how select all text inside <input> or <textarea> element using JavaScript.

Quick solution:

const element = document.querySelector('#text-input');

element.select();

 

Practical example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <input type="text" id="text-input" value="Dirask is awesome!">
  <button onclick="selectText()">Select</button>
  <script>

    var element = document.querySelector('#text-input');

    function selectText() {
        element.select();
    }

  </script>
</body>
</html>

Screenshots:

Before selection: Select all text inside input using select() function using JavaScript.
Before selection: Select all text inside input using select() function using JavaScript.
After selection: Select all text inside input using select() function using JavaScript.
After selection: Select all text inside input using select() function using JavaScript.

See also

  1. JavaScript - select all input text on focus
  2. JavaScript - onselect event example

Alternative titles

  1. JavaScript - select all the text inside textarea
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - DOM Node - problems & solutions

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join