views:

88

answers:

3

How do some sites automatically select a input field when you enter a page? Like YouTube's login page it selects the Username field automatically

This site too, on page Ask Question for example, it selects the Title field.

How do they do this? My guess would be javascript

But how?

+1  A: 

Use:

someObject.focus()

Reference:

http://www.w3schools.com/HTMLDOM/met%5Fanchor%5Ffocus.asp

vmarquez
+3  A: 
<head>
<!-- set focus to a field with the name "searchcontent" in my form -->
<script type="text/javascript">
    function setfocus(a_field_id) {
        $(a_field_id).focus()
    }
</script>
</head>

<body onload="setfocus('customervalue');">

Customer: <input name="customer" id="customervalue" />

</body>

From here: http://lena.franken.de/software/javascript/index.html

glasnt
+1  A: 

In The Future: <input type="text" autofocus> :-)

Not a good idea to use onload on body either, use an onDomReady-event, for instance from YUI or jQuery or some custom script instead.

Jacob R