Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery to Highlight (Select) All Text in a Textbox

I have an input text box with some text in it , onclick event I want to run a javascript function to select (highlight) all text that is in this box , how I can do that with jquery?

like image 453
Spyros Avatar asked Jun 09 '10 11:06

Spyros


People also ask

What does $() do in jQuery?

$(document) wraps a jQuery instance around the document object. ( $ is just an alias for jQuery .)

How do you select all text in HTML?

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

How do I select a text box in HTML?

HTML | DOM Input Text select() Method The DOM Input select() method selects all the text content of a textarea or an input element which contains the text field. Syntax: element. select();


1 Answers

You may take a look at this article:

Let's assume that we have the following text input:

<input type="text" id="txtInput" />

In most cases, we would want to have this feature available to all textboxes across our website, so we would create a function to handle this, and call it as needed. Calling this function with the expected argument will make execute the highlighting of the text.

function selectAllText(textbox) {
textbox.focus();
textbox.select(); }

Assuming you are developing against DotNetNuke 5, or have jQuery already imported into your website, add the following jQuery to your site for each textbox. (If you have a lot of textboxes, we could do this differently, but that's for a different post.)

jQuery('#txtInput').click(function() { selectAllText(jQuery(this)) });
like image 96
Darin Dimitrov Avatar answered Oct 22 '22 23:10

Darin Dimitrov