How do I have the cursor focus to a text field on page load?

January 29th, 2009Read 8541 times

Here I’m explaining how to focus cursor to a text field on page load. You can do this with several methods with a simple javascript code. For this, you have to put a name for your form and your text filed. See the sample form below.

<form name="myForm" method="post" action="">
  <label>
      <input type="text" name="username" />
  </label>
  <label>
      <input type="submit" name="Submit" value="Submit" />
  </label>
</form>

Now just look on the code. You can do this with a single line code within body onLoad.
<body onLoad="document.myForm.username.focus();">
You can call this code with a function like this:
<script>
  function curFocus(){
     document.myForm.username.focus();
  }
</script>
<body onLoad="curFocus();">

Another method is put the code inside the page after the form. See how is it.
<script type="text/javascript" language="JavaScript">
  var focusCtrl = document.forms["myForm"].elements["username"];
       if(focusCtrl) {
           focusCtrl.focus();
   }
</script>

Related Posts with Thumbnails

Recent Posts

  • High Resolution Dock Icons from Asher
  • Northstar Iphone Theme by Jonny Blaze
  • Free Face Avatar by Jojo Mendoza
  • BLACK png icons by Denbas
  • iTunes Minuet Icons Set for Free
  • LG KU580 Mobile Icons for Windows and Mac Free
  • More than 100 Web and UI Elements for Your Design
  • Download Free Designers Tools Icon
  • Deep 2.1 –  An another Free Iphone Icon Set
  • Download Romantic Icon Set Free
  • A Tutorial for Designing a Sleek Audio Player Interface with Photoshop
  • Free Icons for iPhone- Black, Neon and Agua

8 Comments

Add Yours
  1. How do I have the cursor focus to a text field on page load?…

    explaining how to focus cursor to a text field on page load. You can do this with several methods with a simple javascript code…

    Trackback by blogbookmark.com — January 29, 2009 @ 12:07 pm

  2. How do I have the cursor focus to a text field on page load?…

    Explaining how to focus cursor to a text field on page load. You can do this with several methods with a simple javascript code…

    Trackback by Blogsvine — January 29, 2009 @ 12:11 pm

  3. bijusubhash.com » Blog Archive » How do I have the cursor focus to a text field on page load?…

    Explaining how to focus cursor to a text field on page load. You can do this with several methods with a simple javascript code…

    Trackback by linksmarker.com — January 29, 2009 @ 1:02 pm

  4. How do I have the cursor focus to a text field on page load?…

    Explaining how to focus cursor to a text field on page load. You can do this with several methods with a simple javascript code…

    Trackback by PlugIM.com — January 29, 2009 @ 1:08 pm

  5. 1. Assigning to document.unload is poor citizenship. If anything else on your page requires similar handling, you will have to have your onload method do more than set the focus, and a lazy programmer might not even change its name from ‘curFocus’. Instead, use addEvent (IE) or addEventListener (Firefox, etc.)

    1.1. Instead of using conditional code to choose between addEvent and addEventListener, after you’ve learned to do that, consider using a JavaScript library such as Prototype or jQuery.

    2. Adding raw Javascript to your page is messy. Place it in a separate file, and invoke it with a script src=”…” element.

    Comment by Eric Jablow — January 29, 2009 @ 4:22 pm

  6. I read your posts for a long time and must tell that your articles are always valuable to readers.

    Comment by Ex Back — April 9, 2009 @ 6:16 pm

  7. Metallica-Cursors-Download
    Metal Cursor Archives
    Cursors Download

    http://metallicafanatic.blogcu.com/metallica-cursor-download-cursores_4440105.html

    Comment by Cursor — June 12, 2009 @ 5:53 pm

  8. Increddible useful info. I will be back to read other posts also.

    Comment by Carroll B. Merriman — February 12, 2010 @ 10:59 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Subscribe to comments on this post

bijusubhash.com uses Thank Me Later

Security Code: