I have a form which is divided into vertical tabs. I want to dynamically add another tab using Ajax. I've managed to add fields via AJAX and add another

  • item as a vertical tab item, but when I am trying to click it,it does not work. I'm getting TypeError: tab is undefined.

    Here is the code:

        $form['names_fieldset']['add_name'] = array(
        '#type' => 'submit', 
        '#value' => t('Add another section'), 
        '#submit' => array('mymodule_add_more_add_one'), 
        '#ajax' => array(
            'callback' => 'mymodule_add_more_callback', 
            'wrapper' => 'names-fieldset-wrapper', 
        '#limit_validation_errors' => array(), 
    function mymodule_add_more_callback($form, $form_state) {
    $commands = array();
    $current_id = $form_state['num_names'] - 1;
    $subject = drupal_render($form['names_fieldset'][$current_id]['subject']);
    $html = '<li id="group_custom_' . $current_id . '" class="vertical-tab-button vertical-tab-button-custom">' . $subject . '</li>';   
    $commands[] = ajax_command_append(".vertical-tabs-list", $html);
    $commands[] = ajax_command_invoke('.vertical-tabs .vertical-tabs-panes fieldset', 'hide');
    $id = 'node_cv_form_group_custom_' . $current_id;
    $class= 'field-group-tab group-group-custom-' . $current_id . ' required-fields form-wrapper vertical-tabs-pane fieldgroup-effects-processed';
    $body = drupal_render($form['names_fieldset'][$current_id]['body']);
    $html = '<fieldset id="' . $id . '" class="' . $class . '"><div class="fieldset-wrapper">' . $body . '</div></fieldset>';
    $commands[] = ajax_command_append(".vertical-tabs .vertical-tabs-panes", $html);
    return array(
        '#type' => 'ajax',
        '#commands' => $commands,


    function mymodule_add_more_add_one($form, &$form_state) {
    $form_state['rebuild'] = TRUE;}


    The Vertical Tabs are generated by field-group module.

  • 2
    • You don't even have a "tab" variable in there, so it's hard to see how you get a "tab" is undefined error, or perhaps it isn't. Jun 10, 2013 at 1:59
    • Well, I have a cck node, and used group field module, and grouped fields, using Vertical Tab widget. Vetical Tab widget uses Drupal.behaviours.VerticalTab. The issue is that after I load some dynamic fields with AJAX as per example I posted above when clicking a tab I'm getting that error. Sorry for not mentioning that in the main post.
      – rungurean
      Jun 10, 2013 at 6:16


    Your Answer

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

    Browse other questions tagged or ask your own question.