Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn 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 Simpy.

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

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

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.









Uniblue: Free Virus Scan

Search
Find just the answers you seek from among our 1700+ 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
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 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]