Help with cursor automatically in search box on page load?

643 views
Skip to first unread message

Dan

unread,
Feb 7, 2010, 2:33:17 AM2/7/10
to Google AJAX APIs
Does anyone know how to make the cursor automatically appear in the
search box on page load? (like the google.com site)
This would save the step of clicking on the search box.
Thanks.

Vision Jinx

unread,
Feb 7, 2010, 3:08:33 AM2/7/10
to Google AJAX APIs
Hi,

Just give the input box focus

eg.

for this form

<form name="myform">
<input type="text" name="q" />
</form>

you can use something like

window.onload=function(){
document.myform.q.focus();
};


Cheers!
Vision Jinx

Jeremy Geerdes

unread,
Feb 7, 2010, 6:23:18 AM2/7/10
to google-ajax...@googlegroups.com
To visionjinx's response, I would add that, if you're using the standard SearchControl, you can call searchControl.input.focus() to put the cursor in the search box.

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
jrge...@gmail.com

If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!

> --
> You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group.
> To post to this group, send email to google-ajax...@googlegroups.com.
> To unsubscribe from this group, send email to google-ajax-searc...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-ajax-search-api?hl=en.
>

Dan

unread,
Feb 7, 2010, 12:01:27 PM2/7/10
to Google AJAX APIs
Thank you both so much for responding.
I tried searchControl.input.focus() and it worked perfectly.
So awesome.
Thanks again for helping a newb.

On Feb 7, 3:23 am, Jeremy Geerdes <jrgeer...@gmail.com> wrote:
> To visionjinx's response, I would add that, if you're using the standard SearchControl, you can call searchControl.input.focus() to put the cursor in the search box.
>
> Jeremy R. Geerdes
> Effective website design & development
> Des Moines, IA
>
> For more information or a project quote:http://jgeerdes.home.mchsi.com

> jrgeer...@gmail.com

Ruben BlogBorg

unread,
Feb 7, 2010, 4:39:03 PM2/7/10
to Google AJAX APIs
Hi Dan,

A small note to setting input.focus(): if you do that in Firefox in
the onload, it only works if you make use of a timeout...:

function setcursor()
{
document.getElementById('input').focus();
}

setTimeout('setcursor()',200);

Cheers,

Ruben

omr

unread,
Feb 11, 2010, 12:17:07 AM2/11/10
to Google AJAX APIs
Jeremy Geerdes wrote:
> ... if you're using the standard SearchControl, you can call searchControl.input.focus() to put the cursor in the search box.

BTW:

If I'm not mistaken, setting focus on the search box might cause a
branding issue in at least one case: for the Custom Search Control
with 'watermark' branding.

Alternative Custom Search branding options, using static logos
displayed outside of the search box, have long been provided and
documented for the older non-AJAX search form implementations
available for CSEs.

http://code.google.com/intl/en/apis/customsearch/docs/ui.html#other_searchbox

I wonder why such alternative branding options are not currently
provided or documented for use with the Custom Search Control and
Custom Search Element?

-- omr

Blair Carriere

unread,
Oct 3, 2014, 6:11:20 PM10/3/14
to google-ajax...@googlegroups.com, chy...@gmail.com
How can this be accomplished in PHP?

Jeremy Geerdes

unread,
Oct 3, 2014, 11:46:31 PM10/3/14
to google-ajax...@googlegroups.com
You can't do it with only PHP. You need some JavaScript to do it. You have to set up an onload listener which will then find the input and focus on it. Beyond that, it's tough to tell you exactly what to do without more details. And also, given that the Local and Web Search APIs have now been officially discontinued, I would seriously question if it's worth investing any additional time in developing a solution, even based on the remaining portions of what used to be called the AJAX Search API.

jg

--
--
You received this message because you are subscribed to the Google
Groups "Google AJAX APIs" group.
To post to this group, send email to
google-ajax...@googlegroups.com
To unsubscribe from this group, send email to
google-ajax-searc...@googlegroups.com

For more options, visit this group at
You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-ajax-searc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA

If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!
Reply all
Reply to author
Forward
0 new messages