Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, Apple iPhone, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

How do I have the cursor placed in a text field on page load?

Dave, I have added Google to my webpage using the "Google free" code. Can you please tell me how can i by default place the cursor in google search box?


Dave's Answer:

First off, for the edification of other readers, you're refering to my previous article How can I add a Google search box to my Web site?

Now, there are two parts to having a text field (or any HTML form entity) selected when a Web page is loaded, and the key element is to have both the form and the form element named.

Let's have a look, but let's do it with a simpler HTML form for demonstration purposes:

<form method="post" name="myform">
    <input type="text" name="infield" />
</form>

Here you can see that the form is called "myform" and that the specific text field that we'd like to have auto-selected upon page load is called "infield". That's all that needs to be done within the form.

The other half of this is that there needs to be a select associated with the onLoad event, most typically dropped into the actual HTML body tag.

With the named elements shown here, the body tag would simply have added:

<body onload="document.myform.infield.focus()">

That's all there is to it. Now you just need to apply this technique to the Google search form shown in the earlier article. The only change in the form itself is that you need to give it a name:

<form method="get" action="http://www.google.com/search" name="googlesearch">

Since the text field is already called q, all we have to do is add a slight change to the body field, thusly:

<body onload="document.googlesearch.q.focus()">

That should do it!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

Related to your issue of placing the cursor in a text box in drafting an HTML page, my question is how do you STOP it from working. You may have noticed that the MSN start page causes the cursor to start in their search box. I am a google toolbar user and also often type my urls directly into the address bar. The MSN "cursor snag" causes me to wind up typing half my address in the address bar and the other half in the MSN search engine. I usually hit enter before I realize my cursor has been stolen and wind up getting a search result for "ustrating" leaving the "fr" in the address bar! There has got to be a way to regain control of my cursor and KEEP it where I start typing. I suspect that MSN is trying to inflate their search statistics for advertising revenue purposes however it is most certainly a nuisance. Any thoughts?

Posted by: MSN Grrrrrrrrrr at August 25, 2005 6:56 PM

Hi, I just wanted to say: Thanks.

The info i found here really helped me.

tyvm and cya!

Posted by: InF3rN0 at April 28, 2006 4:01 PM

Hi Dave,
I found this advice very helpful, thanks.
The thing I'd like to remind other people about is this.... DON'T FORGET TO REFRESH YOUR BROWSER WHEN CHECKING TO SEE IF THE CHANGES HAVE WORKED!!!!

Posted by: Mark at May 21, 2006 5:30 PM

Excellent tip and much simpler than the 'solution' here http://www.html-faq.com/htmlforms/?entryfocus

Posted by: Suzie at June 30, 2006 9:52 PM

Dave, this site has been a lot of help -- thanks a lot.

Your instructions are pretty clear yet somehow, I just can't get the cursor to be active in the text box by default, when the page loads.

I'm pretty sure it's because I don't know where to include the onLoad instruction. Here's the code I'm working with:

SearchType your query in the box below to launch a Google search in a separate browser window.


Limit search to Stracia.com

... everything works except the cursor is an active by default. Can you help?

Also, I'd really like to have the checkbox to just search *my* site checked by default, as well. How do I do that?

Again, thanks for all your help and great site.
Troy

Posted by: Troy at February 17, 2007 4:24 PM

I'm glad this page came up first when I searched in Google because that was very, very easy!

Posted by: Paul at March 16, 2007 7:58 AM

For a long time now I have wanted to make this simple change to my Google search box on my personal website. This was a huge help and only took about one minute to do. Thanks Dave!

Posted by: Gil at May 23, 2008 8:18 PM

Thank You. The cursor is in the Google search box when I open my site.

Posted by: Johann at June 28, 2009 7:28 AM

Good one...

Posted by: sathik at October 15, 2009 7:14 PM

Please don't change the cursor focus, I hate this, I am changing my homepage from MSN because of this, I always start to type in the url and before I know it half of the url is in the bing search box, it drives me crazy.

Posted by: MSN cursor stealers at February 9, 2010 10:32 AM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.








Ask Dave Taylor: The iPhone App: Advertisement


Uniblue: Get A Free Virus Scan!

Follow me on Twitter @DaveTaylor

Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Book Links
© 2002 - 2010 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.