
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? 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.
Categorized:
HTML and CSS
(Article 4093)
Tagged: Previous: How do I delete an America Online (AOL) Screen Name? Next: Sony PSP system version 2.0 + Web browser? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. 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 PMHi, 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 PMHi Dave, 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 PMDave, 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.
... 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. 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 AMFor 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 PMI have a lot to say, but ...
I do have a comment, now that you mention it!
|
![]()
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!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|