How to add JS to the search form? &lt;?php

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter()
 */
function mymodule_form_alter(&amp;$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'search_form') {

     $form['#attached']['library'][] = 'mymodule/mylibrary';

  }

}


Doing a dpm($form) on the search form, I see it has the my attached library, so what gives?



Solution: 

The above code works fine, turns out it was my js code, I had copied over from another js file and it had an if statement that somehow I missed (it was 4 am). Credit @Alexei Rayu
In Drupal 8, you first need to define assets in a library in your theme or module: https://www.drupal.org/developing/api/8/assets

and then you can attach that library to your form like this:

$form['#attached']['library'][] = 'mymodule/mylibrary';