13
 <input type="submit" class="form-submit" value="" name="op" id="edit-submit">

I want to alter the class from "form-submit" to "form-submitone".

How can do I do it? Where does the class "form-submit" come from?

24

If I were you, I would not change the class name but rather add your own class to the form element. You can do this by implementing hook_form_alter in a custom module or (in case of Drupal 7) in your theme. The code would look something like this:

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id = 'my_form') {
    $form['actions']['submit']['#attributes']['class'][] = 'form-submitone';
  }
}
?>

By the way, the submit button element is not necessarily $form['actions']['submit']. You will have to figure out which element is the submit button in your specific situation. You can do that by outputting the $form variable; I recommend installing the Devel module and adding dsm($form); to the function above.

5
  • how do you know this can give a class to the submit button.$form['actions']['submit']['#attributes']['class'][]
    – enjoylife
    May 23 '11 at 9:51
  • 2
    It's in the Forms API reference, api.drupal.org/api/drupal/…. May 23 '11 at 10:01
  • the api shows as this $form['#attributes'] = array('class' => array('search-form')); why you write as this .$form['actions']['submit']['#attributes']['class'][]. and have a []? thank you
    – enjoylife
    May 23 '11 at 10:04
  • 7
    If you would do $form['actions']['submit']['#attributes']['class'] = array('form-submitone');, you would be overriding all classes that are already set. If you would do $form['actions']['submit']['#attributes'] = array('class' => array('form-submitone'));, you would overwrite not only the existing classes, but all attributes. (PS See also my addition to the answer above.) May 23 '11 at 10:40
  • This works in Drupal 8 too. Mar 24 '20 at 10:02
0

this code basically user dor add your custom class on form submit button.

function subscriber_form(){
    $form['submit'] = array(
    '#type'  => 'submit',
    '#attributes'=>array('class'=> array('mybtn')),
    '#ajax' => array(
      'callback' => 'subscriber_ajax_callback',
    ),
    '#value' =>'Submit', // @FIXME doesn't render quotes properly
  );

}
2
  • 2
    this is not correct, the "class" item in the #attributes array needs to be an array: 'class' => array("mybtn-class", "mybtn-class2")
    – DrCord
    Aug 26 '15 at 16:52
  • Although @devendra answer works, following @DrCord suggestion, it should be '#attributes' => array('class' => array('mybtn', 'mybtn-2')) Nov 25 '17 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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