views:

23

answers:

2

I have a form with a drop down menu and I want to do a javascript action whenever the user changes the selection. I imagine it is possible find the input later, using javascript, and attach an event to to it; but it seems like it would be easier if there was some sort of attribute or option that could be defined in form->configure(): e.g.

$this->widgetSchema['menu'] = new sfWidgetFormChoice(array(
  ...
  'onclick' => javascript function
));

obviously this doesn't work, and it probably wouldn't be an onclick method either, but my question is how do you attach a javascript event to a input/widget?

+1  A: 

Every element that a form object creates should have an autogenerated id, which is usually in the form:

form_name_field_name

So how I do this, using jQuery as an example:

$('#form_name_field_name').change(function(){
  alert('do some cool stuff with '+this.val());
});
johnwards
A: 

You can see it : How to use javascript in forms

Corum