Focus and Select a Form Input

/ Published in: JavaScript
Save to your folder(s)

For forms with a default value onload you don't want your users to have to select the value, delete it and then insert their own input.

This function focuses on a selected input, then it selects the default text so the user can easily type their own value.

Copy this code and paste it in your HTML
  1. /** @id codeFocus
  2. * @classDescription Focuses on the selected input and selects the value text.
  3. */
  4. var postcodeFocus = {
  5. grab:function(){
  6. if(!document.getElementById){return;};
  7. if(!document.getElementById('selected')){return;};
  8. var theInput = document.getElementById('selected');
  9. theInput.focus();
  11. }
  12. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.