ZéSá
ZéSá

Reputation: 19

CakePHP onChange event

I have no idea why this onChange event is not working. Maybe I got so used to the code that I can't see my mistake. I'd appreciate your help:

<td class="cellInput">
  <?php
    $options = array('200'=>'200', '500'=>'500', '1000'=>'1000', '2000'=>'2000', '5000'=>'5000', 'Outro'=>'Outro');
    $attributes = array('legend' => false);

    echo $form->select('capacidade', $options, array(
      'class' => '',
      'label' => '',
      'default' => '200',
      'onchange' => "javascript:checkForOther(this);",
    )); 
  ?>
</td>

Upvotes: 0

Views: 12642

Answers (3)

bbstyle
bbstyle

Reputation: 21

As others suggested, place the attributes in the correct parameter position. The other thing i would do is remove the javascript and just have the function name in there like:

From: 'onchange' => "javascript:checkForOther(this);"

To: 'onchange' => "checkForOther(this)"

Upvotes: 2

elitalon
elitalon

Reputation: 9447

According to CakePHP documentation on select method, the third argument is used to choose which option is selected by default.

You must use the fourth argument to pass HTML attributes to a select element:

<td class="cellInput">
  <?php
  $options = array('200'=>'200', '500'=>'500', '1000'=>'1000', '2000'=>'2000', '5000'=>'5000', 'Outro'=>'Outro');
  $attributes = array('legend'=>false);
  echo $form->select('capacidade', $options, '200', array('class'=> '', 'label' => '', 'onchange' => "checkForOther(this);")); 
  ?>
</td>

Upvotes: 0

abhinav
abhinav

Reputation: 3217

Try

  1. Putting the attributes as the fourth argument instead of the third:

    echo $form->select('capacidade', $options, null, 
        array(
            'class'=>''
            ,'label' => ''
            ,'default' => '200'
            ,'onchange' => "javascript:checkForOther(this);"
        )
    );
    
  2. Does the source of the generated page have the onChange attribute?

  3. See the documentation

Upvotes: 0

Related Questions