title: Yii2 button onclick anonymous function
body: <p>I am new to Yii2, and I am struggling to trigger an anonymous function by pressing a Yii2 button.
  Below are 6 samples, of which first two are OK. 
  But that is not exactly what I wish to have. 
  I would like to know how I can get an anonymous function working, like cases for "Button 3" and "Button 5". I tested how to make a function call via Controller, and that works ok, but that is neither what I want. I would appreciate Your help - thanks!</p>
  
  <pre><code>// This works
  $button1 = Button::begin ( 
  [
  'label' =&gt; 'Button 1',
  'options' =&gt; [
      'class' =&gt; 'btn btn-primary',
      'onclick' =&gt; 'alert("Button 1 clicked");',
      ],
  ]);
  $button1-&gt;run();
  
  // This works
  echo Html::button('Button 2', [ 'class' =&gt; 'btn btn-primary', 'onclick' =&gt; 'alert("Button 2 clicked");' ]);
  
  // This DOES NOT work
  echo Html::button('Button 3', [ 'class' =&gt; 'btn btn-primary', 'onclick' =&gt; 'function ( $event ) { alert("Button 3 clicked"); }' ]);
  
  // This DOES NOT work
  $button4 = Button::begin ( 
  [
  'label' =&gt; 'Button 4',
  'options' =&gt; [
      'class' =&gt; 'btn btn-primary',
      // 'onclick' =&gt; 'alert("Button 1 clicked");',
      ],
  ]);
  $button4-&gt;on('onclick', 'alert("Button 4 clicked");' );
  $button4-&gt;run();
  
  
  // This DOES NOT work
  $button5 = Button::begin ( 
  [
  'label' =&gt; 'Button 5',
  'options' =&gt; [
      'class' =&gt; 'btn btn-primary',
      'onclick' =&gt; 'function ( $event ) { alert("Button 5 clicked"); }',
      ],
  ]);
  $button5-&gt;run();
  
  // This DOES NOT work
  $button6 = Button::begin ( 
  [
  'label' =&gt; 'Button 6',
  'options' =&gt; [
      'class' =&gt; 'btn btn-primary',
      //'onclick' =&gt; 'function ( $event ) { alert("Button 4 clicked"); }',
      ],
  ]);
  $button6-&gt;on('onclick', 'function ( $event ) { alert("Button 6 clicked"); }' );
  $button6-&gt;run();
  </code></pre>