How to add cursor to text field on page load? I'm working on an attendance site in Drupal 7 and I use a reference entity field to accept scanned ID badges. Once the ID number is saved in the field the page reloads.

I'd like to have a cursor appear in the field after the page is reloaded. I know this can be done with java script but I haven't been able to get it to work. I added scripts[] = scripts.js in the .info of the theme and created a .js file with the same name in my theme directory.

(function setFocus() {
document.getElementById("field_student").focus();


}

function select() {
    document.getElementById("field_student").select();


}

I'm not very adept at javascript and I'd appreciate it if someone could point me in the right direction.
You can actually do it with html autofocus now - no need for JS.

You should be able to do it with this:

&lt;?php
  // inside a hook_form_alter()      

  $form['field'] = [
    '#type' =&gt; 'textfield',
    '#attributes' =&gt; array('autofocus' =&gt; TRUE)),
  ];