What is a Vanilla JS or jQuery solution that will select all of the contents of a textbox when the textbox receives focus?
The HTMLInputElement. select() method selects all the text in a <textarea> element or in an <input> element that includes a text field.
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.
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.
$(document).ready(function() { $("input:text").focus(function() { $(this).select(); } ); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With