Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all contents of textbox when it receives focus (Vanilla JS or jQuery)

What is a Vanilla JS or jQuery solution that will select all of the contents of a textbox when the textbox receives focus?

like image 817
Jon Erickson Avatar asked Jan 26 '09 18:01

Jon Erickson


People also ask

How do you select all input fields of text?

The HTMLInputElement. select() method selects all the text in a <textarea> element or in an <input> element that includes a text field.

What does focus do in jQuery?

The focus() is an inbuilt method in jQuery which is used to focus on an element. The element get focused by the mouse click or by the tab-navigating button. Here selector is the selected element. Parameter: It accepts an optional parameter “function” which specifies the function to run when the focus event occurs.

What is focus in textbox?

Textbox. Focus() "Tries" to set focus on the textbox element. In case of the element visibility is hidden for example, Focus() will not work. So make sure that your element is visible before calling Focus() . Follow this answer to receive notifications.


1 Answers

$(document).ready(function() {     $("input:text").focus(function() { $(this).select(); } ); }); 
like image 113
John Sheehan Avatar answered Sep 22 '22 21:09

John Sheehan