How to select the content of an input field

It's quite easy to select the content of an input field or a textarea whenever it's clicked or focus. You just simply call select() method in the onclick and onfocus event handlers of the input/textarea element. The function of select() method is to select the content of the element. Below is the sample HTML code.

<input name="textBox1" id="textBox1" value="This text will be select, it is clicked." onclick="javascript: this.select();" onfocus="javascript: this.select();" type="input" />

Example


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><img> <object> <embed> <param>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.
ญาณรักข์ วรรณสาย