I needed to adjust values between form fields during user data entry, so I hacked my copy of the textfiled.inc to add this functionality.

I added this to the _webform_edit_textfield function:

  $edit_fields['extra']['attributes']['onChange'] = array(
    '#type' => 'textfield',
    '#title' => t("onChange"),
    '#default_value' => $currfield['extra']['attributes']['onChange'],
    '#description' => t('To add an onChange javascript trigger to this field include the function call here. Leave blank to ignore.'),
    '#size' => 30,
    '#maxlength' => 128,
    '#weight' => 4,
  );

Usage:
- create the fields. Note their ID's if necessary.
- create a "markup" field, put your dynamic form javascript functions there.
- update the field(s) you want to trigger the onchange actions by putting javascript in the new "onChange" field on the textfiled edit page.

Issues:
- adds onchange='' to every text field. I don't know how to fix this. Not a problem for me, but not very tidy...
- created for 5x-1.4 as this is what we use. YMMV with other versions.

Have fun :-)

Comments

quicksketch’s picture

This doesn't seem entirely necessary to me, especially because you need to have some custom JavaScript on the page in order to call it. The goal looks like you just wanted an onchange attribute. You can achieve the same thing more easily by using jQuery in your custom JS files:

$('#edit-submitted-mytextfield').change(myCustomCallback);

function myCustomCallback() {
  // Do stuff on change.
}
quicksketch’s picture

Status: Needs work » Closed (works as designed)

I'm marking by design because jQuery provides this exact functionality, without dirtying up the HTML with onclick attributes.