View Full Version : Start the cursor in the search box
jarvi
May 16th 2004, 12:03 am
I have a very basic php page that really just displays the standard menu items for the site plus a search box using a form and submit button. How do I get it so that the cursor is in the search box and the user can just start typing, rather than having to click in the box first?
Google does it if you can't understand what I am saying.
nlopes
May 16th 2004, 3:47 am
This is just a simple javascript code.
In <head> add this:
<script>
<!--
function focus(){document.FORM_NAME.INPUT_NAME.focus();}
// -->
</script>
And then in <body>:
<body onLoad=focus()>
jarvi
May 17th 2004, 2:02 am
Thanks, mate. Knew it wold be something simple.
webnp05
Jul 14th 2005, 9:18 pm
Does this work with Safari on a mac?
J.D.
Jul 15th 2005, 9:43 pm
Does this work with Safari on a mac?This syntax is not standard - only elements in HTML collections can be accessed using this notation (i.e. using id's as JS identifiers). This syntax will work in all DOM-compliant browsers:
document.getElementById("input-id").focus();
input-id must be the value of the ID attribute of the input element, not name.
J.D.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.