I am new to drupal and I have a problem in drupal text-box.

Question : how to give a class for text box?

my html :

<input type="text" name="uname" class="uclass" id="uid">

i want the same in drupal

My code is :

function my_module_my_form($form_state) {

// This is the first form element. It's a textfield with a label, "Name"

$form['name'] = array(

'#type'  => 'textfield',
                    '#title' => t('Enter Your Name'),
                    '#value' => t('Enter user name'),
                    '#name'  => 'uname',
                    '#id'    => 'uid',
                    '#class' => 'uclass',
                    '#maxlength' => 5
return $form;


Note: through inspect element , i find the class name is "form-text", but actually my class name is uclass, It is not taking, can one help me.

1 Answer 1


Additional HTML attributes, such as 'class', can be added using the '#attributes' property.

function my_module_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
   $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Enter Your Name'),
      '#weight' => -10,
      '#attributes' => array('class' => array('uclass')),

Please note that when you alter existing form elements (e.g. via hook_form_alter()), you probably want to add (attributes) to the existing array rather than writing over it.

Checkout Form API of Drupal 7 to get more detail.

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.