By Matt Murphy on Mar 7, 2008


This function, when called on a particular HTML element, will auto-select all of its inner text. It works on elements such as input, textarea, div, span, td, and pre.

Just click in one of the areas below for a demonstration.

A div Element:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam ultrices vestibulum elit. Mauris congue sapien sed dolor. Pellentesque sem augue, porttitor id, placerat ac, congue ac, eros. Etiam fermentum consectetuer pede. Donec tincidunt. Suspendisse non nisi. In hac habitasse platea dictumst. In hac habitasse platea dictumst. Integer porta egestas sapien.

An input Element:

A textarea Element:

A pre Element:

function toggle_visibility(id) {
  var e = document.getElementById(id);
  if( == 'none') = 'block';
  else = 'none';


  • Created by: Matt Murphy
  • Web Site:
  • Posted: March 07, 2008

Source Code

Paste this source code into the designated areas.

External file

Paste this code into an external JavaScript file named: autoSelect.js


Paste this code into the HEAD section of your HTML document.


Paste this code into the BODY section of your HTML document


Blog comments powered by Disqus