JavaScript: How to target & set focus on a text input field?
Asked by
makemo (
531)
April 25th, 2008
I recently registered an new domain and with it, I got this Google Apps startpage for my domain.
Probllem is, that when I open a new Google search start page, the text input field never gets focused, as it did when I was using the generic Google seach page (iGoogle).
So I’m trying to figure out a JavaScript to locate the specific target element for that text (search) field and add it in the start page URL, so that I can just type away without having to actually click in the text input field, each time I open a new start page.
I’m highly new to JavaScript, so any help would be much appreciated. FWIW my startpage is http://start.henrikcederblad.com/ (I don’t know if its viewable by the public, but I think it is, yes?).
Observing members:
0
Composing members:
0
6 Answers
If the form looks something like this:
<form name=“form”>
<input type=“text” name=“textfield”>
</form>
Add this to your body tag.
<body OnLoad=“document.form.textfield.focus();”>
edit :: when viewing your page source I wasn’t getting any line breaks. I didn’t want to dig thought that to find exactly what you should add.
Give the input box an id like “startbox”
Then on the body tag add the attribute.
onLoad=“document.getElementById(‘startbox’).focus();”
Use what Bree said. That is a better solution.
FWIW, here is the actual HTML structure for the DIV containing the search field:
<div id=“gsea”>
<form method=“get” action=“http://www.google.com/custom”>
<table>
<tbody>
<tr>
<td valign=“top” height=“32” nowrap=“nowrap” align=“left”><input name=“q” size=“35” maxlength=“255” type=“text” /> <input name=“sa” value=“Google Sök” type=“submit” /><input type=“hidden” name=“client” value=“ca-ref-pub-9868490492756260” /><input type=“hidden” name=“cof” value=“FORID:1” /><input type=“hidden” name=“hl” value=“sv” /><input type=“hidden” name=“ie” value=“UTF-8” /><input type=“hidden” name=“oe” value=“UTF-8” /></td>
</tr>
</tbody>
</table>
</form>
</div>
Look through the Fluther Javascript. We do this when you click the login/join links.
Answer this question
This question is in the General Section. Responses must be helpful and on-topic.