Focus and select text in an Input field

Description Javascript

This function not only focuses the cursor to an input field, but also selects all text within.

May cause errors if supplied element is not an text, password or textarea field.

Requires xGetElementById (also available on this site).

The Code Download

  1. function jFocus(elm) {
  2.     if(typeof(elm) == 'string') {
  3.         elm = xGetElementById(elm);
  4.     }
  5.     if (elm) {     
  6.         elm.focus();
  7.         elm.select();
  8.     }
  9. }

Credits

Added by JC on 13th November 2007

Comments

There are no comments about this snippet.

Post Comment Github Markdown Supported

You must be logged in to post a comment.

Login here to post a comment