Using Drupal 7 and Webform (both up to date), I'm looking for a way to add a button that when clicked, set the value of a field.

I can add a button using code like this:

function mymodule_form_alter(&$form, &$form_state, $form_id) {

switch ($form_id) { 

case 'webform_client_form_ID':
  $form['alt_button'] = array(
  '#type' => 'submit',
  '#value' => t('Alternative Action'),

I now need to figure how to perform the action of changing the value of one of the the webform field when this button is clicked...

Any help will be appreciate!

1 Answer 1


You will have to create a button in a

function hook_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
    // Your code here.

and use the #ajax property of the button to update the field when the button is clicked.

The examples D7 module has some nice AJAX examples that will show you how the mechanism works.

  • Thank you very much! It seems full of sense, but way too complicated for me...
    – Fumble
    Jan 12, 2017 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.