- How to Select All Text in HTML Text Input When Clicked Using JavaScript
- How To Select All Text In The Input Field When It Is Clicked
- Video Tutorial:
- Project Folder Structure:
- HTML:
- CSS:
- Javascript:
- Selecting the all text in HTML text input using JavaScript
- JavaScript Select All Text in Div with a Mouse Click
- Alternative Solution: Use CSS
- Careful with Content Editable Elements!
- HTMLInputElement: select() method
- Syntax
- Parameters
- Return value
- Examples
- HTML
- JavaScript
- Result
- Notes
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
How to Select All Text in HTML Text Input When Clicked Using JavaScript
It is pretty simple to select whole text on just a single click. You can use the following JavaScript code snippet:
html> html> head> title>Title of the Document title> head> body> div> Input Text: input onClick="this.select();" type="text" value="Sample Text"> div> body> html>
However, it does not work on mobile Safari. In such cases, you can use:
html> html> head> title>Title of the Document title> head> body> div> Input Text: input onClick="this.setSelectionRange(0, this.value.length)" value="Sample Text" /> div> body> html>
The HTMLInputElement.select() method selects the entire text in a element or element that includes a text field.
But it becomes impossible to place the cursor at a desired point after focus. Here is another solution that combines all text selection on focus and as well as allows selecting a specific cursor point after focus:
html> html> head> title>Title of the Document title> script> head> body> div> Input Text: input id="input-tag" value="Sample Text" /> div> script> const inputElement = document.getElementById('input-tag'); inputElement.addEventListener('focus', function(e) < inputElement.select() >) script> body> html>
The HTMLElement.focus() method sets focus on the given element if it can be focused. By default, the focused element will receive keyboard and similar events.
How To Select All Text In The Input Field When It Is Clicked
Hey everyone. Welcome to today’s tutorial. In today’s tutorial, we will learn how to select all text in a text input when we click on it. To create this we use HTML, CSS and Javascript.
Video Tutorial:
If you are interested to learn by watching a video tutorial rather than reading this blog post, you can check out this tutorial here down below. Also, check out my youtube channel where I post new tutorials regularly. I also keep you updated with the latest tips and tricks related to web development.
Project Folder Structure:
Now before we start coding let us create the project folder structure. We create a project folder called- ‘Select Text In Input Field On Click’. Inside this folder, we have three files. These files are index.html, style.css and script.js. They are the HTML document, the stylesheet and the script file.
HTML:
We start with HTML files. First, copy the code below and paste it into your HTML document.
The HTML document consists of a div with the class name ‘container’. Inside the container, we have the input element. The only purpose of the container is to centre and wrap the input element.
CSS:
Next, we use CSS to style the container and input text elements. We start by removing unnecessary paddings and margins from all the elements. Following this, we set ‘#03cc64’ as the background colour for the body.
Next, we add paddings and font size to the input element style even more.
* < padding: 0; margin: 0; box-sizing: border-box; >body < background-color: #03cc64; >.container < background-color: #ffffff; width: 80vmin; position: absolute; transform: translate(-50%, -50%); top: 50%; left: 50%; padding: 50px 0; border-radius: 8px; box-shadow: 0 20px 45px rgba(1, 51, 25, 0.2); >.container input < display: block; width: 60%; margin: auto; border: 1.6px solid #222537; font-family: "Poppins", sans-serif; font-size: 20px; padding: 15px 10px; border-radius: 5px; outline: none; >.container input:focus
Javascript:
Lastly, we add functionality using Javascript. We declare a variable called ‘demoInp’ and initialize it to the input text element.
Next, we add a ‘click’ event listener to this input element. Through the function that is called on click, we select the value of this input field.
let demoInp = document.getElementById("demo-inp"); demoInp.addEventListener("click", function () < this.setSelectionRange(0, this.value.length); >);
That’s it for this tutorial. If you have any issues while creating this code you can download the source code by clicking on the download button below. Also, if you have any queries, suggestions, or feedback you can comment below.
Happy Coding!
Selecting the all text in HTML text input using JavaScript
In this tutorial, we are going to learn about how to select all text entered in an HTML input (field) when a button is clicked using JavaScript.
Consider, we have the following html input field, button element.
input id="place" value="King towers" /> button id="select">Select Textbutton>
To select all text present inside a text input field when a button is clicked, first we need to access the above two elements inside a JavaScript by using the document.getElementById() method.
const input = document.getElementById("place"); const btn = document.getElementById("select");
Now, we need to attach a click event listener to the btn element, inside that we can select the input text by using the input.select() method.
btn.addEventListener("click",()=> input.select(); // it selects the text >)
We can also select all text in an input field by clicking on the input field itself.
input id="place" value="King towers" onclick="this.select()" />
Similarly, we can use the input.setSelectionRange() method by passing 0, input.value.length as an first and second arguments.
const input = document.getElementById("place"); const btn = document.getElementById("select"); btn.addEventListener("click", ()=> input.focus(); // it focusses the text input.setSelectionRange(0, input.value.length); >)
JavaScript Select All Text in Div with a Mouse Click
To select all div text with a single mouse click in JavaScript, you can use the following code:
Click this paragraph once and it's highlighted by JavaScript!
function highlight(id) < if (document.selection) < var range = document.body.createTextRange(); range.moveToElementText(document.getElementById(id)); range.select(); >else if (window.getSelection) < var range = document.createRange(); range.selectNode(document.getElementById(id)); window.getSelection().removeAllRanges(); window.getSelection().addRange(range); >>
Here’s what the JavaScript function does step-by-step:
- The function checks whether the document.selection property exists. This property is only available in older versions of Internet Explorer.
- If document.selection exists, the function creates a new text range and positions it to surround the text within the element with the ID id . It then selects the text range using the select() method.
- If document.selection does not exist, the function checks whether the window.getSelection() method exists. This method is available in modern browsers.
- If window.getSelection() exists, the function creates a new range that encompasses the element with the ID id . It then removes any existing selections using the removeAllRanges() method and adds the new range to the user’s selection using the addRange() method.
Update: Because Internet Explorer is no longer in use, you might just want to remove the if-else function and use the code inside the else block.
Test the above code solutions below by clicking the text element:
Alternative Solution: Use CSS
To make an HTML div text selectable with a single click of a button, you can use CSS. This way you don’t have to use JavaScript at all.
To pull this off, you can use the user-select specification as follows.
Click this paragraph once and it's highlighted by CSS!
You can test this solution by clicking the paragraph below:
Careful with Content Editable Elements!
Content editable elements are HTML elements that can be edited by the user, such as div , span , and p elements. These elements have the contenteditable attribute set to “true”. When an element is content editable, the user can interact with its contents as if they were editing a document in a word processor.
When using JavaScript to select text in a content editable element, it’s important to use the selectNodeContents method instead of selectNode . The selectNodeContents method selects all of the contents of the element, whereas selectNode only selects the element itself.
This is some editable text. Select me!
function selectEditableText(element)
In this code, we define a function called selectEditableText that takes one argument, element . This function creates a new range that encompasses all of the text within the element , selects that range, and adds it to the user’s selection.
In the HTML, we add an onclick attribute to the content editable div and pass this as an argument to the selectEditableText function. The this keyword refers to the element that triggered the event, which in this case is the content editable div .
Because the element is content editable, we use the selectNodeContents method to ensure that all of the text is selected.
You can try the solution by clicking the content editable div below:
Thanks for reading. Happy coding!
HTMLInputElement: select() method
The HTMLInputElement.select() method selects all the text in a element or in an element that includes a text field.
Syntax
Parameters
Return value
Examples
Click the button in this example to select all the text in the element.
HTML
input type="text" id="text-box" size="20" value="Hello world!" /> button onclick="selectText()">Select textbutton>
JavaScript
function selectText() const input = document.getElementById("text-box"); input.focus(); input.select(); >
Result
Notes
Calling element.select() will not necessarily focus the input, so it is often used with HTMLElement.focus .
In browsers where it is not supported, it is possible to replace it with a call to HTMLInputElement.setSelectionRange() with parameters 0 and the input’s value length:
input onClick="this.select();" value="Sample Text" /> input onClick="this.setSelectionRange(0, this.value.length);" value="Sample Text" />
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Apr 7, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.