« Back to blog

Focus on inputfield with Javascript

Handy for example a login form where users automatically get their cursor focussed on the input field where they should enter their credentials.

<script type='text/javascript'>
window.onload = function() { //start the javascript when the page loads
  document.getElementById("focusonme").focus(); //here is the id you also set in your input field
}
</script>

Remember to set an id for the input field where you want the cursor to focus on.
<input type='text' name='name' id='focusonme'>

Easy! :)