How do I enable an onclick event in a link on a form? I am using the following code.

 $form['test'] = array(
        '#type' =&gt; 'link',
        '#href' =&gt; "javascript:;",
        '#title' =&gt; t('Show'),
        '#attributes' =&gt; array(
            'onclick' =&gt; "show_this();",
            'onsubmit' =&gt; 'return false',
        ),
        '#id' =&gt; 'myid',
  );


Its output is the following.

&lt;a href="/javascript%3A%3B" onclick="show_this();" onsubmit="return false" id="myid"&gt;Show&lt;/a&gt;


The onclick event is working properly. I want to disable href.

How can I achieve this?
You don't need the #href to prevent the event.  

$form['test'] = [
  '#type' =&gt; 'link',
  '#href' =&gt; '#',
  '#title' =&gt; t('Show'),
  '#attributes' =&gt; [
    'onclick' =&gt; "alert('hello');return false",
  ],
  '#id' =&gt; 'myid',
];


In the above example, I removed the #href and the onsubmit handler and added return false after the alert() function in the onclick handler.
I don't know what  you're trying to achieve in your code, but the above example is working for me.  



UPDATE:
I update the #href in my answer.