[development] Drupal way of doing onchange

Greg Holsclaw greg at t2media.com
Sun Nov 22 21:25:11 UTC 2009

A quick way to do this is to use the attributes as you mentioned. Any HTML
attribute can be added into the attributes array as follows:

  $form['cc'] = array (
    '#attributes' => array(
	'onChange' => "some_js_funtion();"

You can include Jquery directly there instead of the function if you simply
need to hide a div, or you can use:


Of course, from a JavaScript purists mindset, you should attach all the
event handlings later, but that is a completely different topic.

If you have a callback to get more information from a menu callback, then
AHAH or CTOOLs is definitely the way to go, but for simpler affects this way
might be sufficient.


-----Original Message-----
From: development-bounces at drupal.org [mailto:development-bounces at drupal.org]
On Behalf Of Jeff Greenberg
Sent: Sunday, November 22, 2009 10:38 AM
To: development at drupal.org
Subject: [development] Drupal way of doing onchange

Haven't delved into AHAH yet, this might force the issue.

Using an easy (=less typing) example. A credit card form. When the 
credit card type is selected from a select, the selection needs to fire 
a javascript function that will disable fields that don't apply to that 
card type (such as CVV).

Normally, I would have an onchange event for the select, and noticed 
that in the form api, while there is an attribute section to add 
miscellaneous attributes, the doc doesn't mention events as examples. 
And if putting an event there is legitimate, there isn't a mention of 
how to include the responding js function in the form.

If AHAH is the way I'm supposed to handle this, I'd appreciate being 
pointed to a good similar example, because I'm a bit unclear from the 
AHAH entries in the form reference docs, for example, how to relate the 
myhandler/js entry to a physical location of the function, and how the 
function gets there.


More information about the development mailing list