Forum Moderators: open

Message Too Old, No Replies

Input field

         

bodycount

11:41 am on Dec 8, 2006 (gmt 0)

10+ Year Member



This is will be a quick one for someone.

How do i make a input field have the cursor in it, on a page load, so that i dont have to move/click the mouse before i can enter any data into it.

coopster

4:47 pm on Dec 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You'll want to focus on the element using the 'onload' attribute of the <body> element.
<body onload="document.getElementById('firstName').focus();">

Fotiman

5:19 pm on Dec 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



But do it in a non-obtrusive way, like this:


<head>
...
<script type="text/javascript">
function init()
{
var el = document.getElementById('firstName');
if( el ) el.focus();
}
window.onload = init;
</script>
</head>
<body>
...
<input type="text" id="firstName">
...
</body>