ShaGGy
ShaGGy

Reputation: 3

focus on page load (works if refreshed)

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

Answers (3)

Shibu Thomas
Shibu Thomas

Reputation: 3196

$( "#keywords" ).focus(function() {
  alert( "Handler for .focus() called." );
});

Upvotes: 0

GuyT
GuyT

Reputation: 4416

First include jQuery, then:

$(document).ready(function(){
    $('#keywords').focus();
})

Upvotes: 0

adeneo
adeneo

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

Related Questions