Hi,

i need to add a css class to the form tag of my webform(s).

But i have absolutely no idea how to get it done.

Can anyone help me please?

Thanks for reading this!

Comments

edrupal’s picture

You could try using hook_form_alter.

In your theme's template.php file you would add a function something like:

function mytheme_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'id-of-form') {
    $form['#attributes']['class'][] = 'my-custom-class';
  }
}

To get the id-of-form, you can 'inspect' the html of the page and if you take the id of the form and convert the dashes to underscores, this will be the id-of-form. So using the html example below, the id-of-form is webform_client_form_1

<form id="webform-client-form-1" class="webform-client-form myclass" accept-charset="UTF-8" method="post">

Hope this helps

deelite’s picture

Thank you.

I have another form_alter in my template.php so i had to combine both functions.

Both in a separate function brings a blank page.

I use this code:

function d03_form_alter(&$form, &$form_state, $form_id) {
  if (!empty($form['actions']) && $form['actions']['submit']) {
    $form['actions']['submit']['#attributes'] = array('class' => array('first', 'button', 'radius'));
  }
  if ($form_id == 'webform-client-form-4') {
    $form['#attributes']['class'][] = 'custom';
  }
}

But it doesn't work.

For testing i removed the first function, buth with no success.

Could you please have a look over the code?

And is it possible to get the function for all forms without having a function for every single one?

edrupal’s picture

The code in your example looks fine except in the 'if' test the webform id needs to use underscores. ie.

if ($form_id == 'webform_client_form_4') {
    $form['#attributes']['class'][] = 'custom';
  }

Ed

deelite’s picture

It works!

Thank you so much, Ed!

joshuautley’s picture

Awesome! Thank you both.

Josh Utley, President & CEO
Intrepid Network Inc. | Multimedia Services & Business Solutions

"Our mission is to provide a more personable multimedia service along with the highest quality business solutions."