Reputation: 3
How can I make my page focus automatically on the field
<script>
function focus(event) {
document.getElementById('keywords').focus();
}
</script>
<BODY onload="focus(event)"></body>
appears to work but only if i refresh the page is there a way to make it focus automatically on the page load.
Upvotes: 0
Views: 2318
Reputation: 3196
$( "#keywords" ).focus(function() {
alert( "Handler for .focus() called." );
});
Upvotes: 0
Reputation: 4416
First include jQuery, then:
$(document).ready(function(){
$('#keywords').focus();
})
Upvotes: 0
Reputation: 318192
Just use the autofocus attribute, no javascript needed for this ?
<input id="keywords" autofocus="autofocus" />
There doesn't seem to be anything wrong with the code you have, so there must be something else going on if it's not working.
Upvotes: 1