Languages
[Edit]
EN

JavaScript - select all text inside input

0 points
Created by:
Adnaan-Robin
274

In this article, we would like to show you how to select all the text in the <input> element or <textarea> using select() function in JavaScript.

Quick solution:

// get element by id and select with select() function
function selectText() {
  const element = document.querySelector('#text-input');
  element.select();
}

 

Practical example

In this article, we get the text-input element by id and select all the text from it using select() function.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
  <body>
    <input type="text" id="text-input" value="Dirask is awesome!">
    <button onclick="selectText()">Select.</button>
    <script>
      function selectText() {
        const element = document.querySelector('#text-input');
        element.select();
      }
    </script>
  </body>
</html>

Result:

JavaScript - select all text inside input using select() function - before
JavaScript - select all text inside input using select() function - before
JavaScript - select all text inside input using select() function - after
JavaScript - select all text inside input using select() function - after

Related posts

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