How to add cursor to text field on page load?
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)),
  ];