Add more fieldset with remove option custom module I'm developing custom module and there's one fieldset that can be added multiple times and can be removed any particular entry also. Can anyone please suggest to achieve this? Here is my code:

function hook_form($form,&amp;$form_state){
$form_state['storage']['emails'] =
isset($form_state['storage']['emails']) ? $form_state['storage']['emails'] : 0;

$form['emails'] = array(
    '#type' =&gt; 'container',
    '#tree' =&gt; TRUE,
    '#prefix' =&gt; '&lt;div id="emails"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',

);
if(empty($form_state['storage']['emails'])) {
    $form_state['storage']['emails'] = 0;
}
for ($j = 0; $j &lt;= $form_state['storage']['emails']; $j++) {
    $form['emails'][$j]['candidate_email'] = array(
        '#title' =&gt; 'Email',
        '#type' =&gt; 'textfield',
        '#size' =&gt; '30',
        '#required' =&gt; FALSE,
    );
}
$form['emails']['add_email'] = array(
    '#type' =&gt; 'button',
    '#value' =&gt; t('Add Email'),
    '#href' =&gt; '',
    '#ajax' =&gt; array(
        'callback' =&gt; 'hook_ajax_add_emails',
        'wrapper' =&gt; 'emails',
    ),
);
$form_state['storage']['emails']++;

$form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; 'Submit',        
);
return $form;
}

function hook_ajax_add_emails($form, $form_state) {
    return $form['emails'];    
}
I got solution by this way. Here is solution code:

$form['emails'] = array(
    '#title' =&gt; 'Education Information',
    '#type' =&gt; 'fieldset',
    '#collapsible' =&gt; TRUE,
    '#collapsed' =&gt; FALSE,
    //'#size' =&gt; '30',
    '#required' =&gt; FALSE,
    '#prefix' =&gt; '&lt;div id="names-fieldset-wrapper"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
);
if (empty($form_state['num_names'])) {
    $form_state['num_names'] = 1;
}
for ($i = 0; $i &lt; $form_state['num_names']; $i++) { 
$form['emails'][$i]['candidate_email'] = array(
    '#title' =&gt; 'Email',
    '#type' =&gt; 'textfield',
    '#size' =&gt; '30',
    '#required' =&gt; FALSE,
);
}

$form['emails']['add_name'] = array(
'#type' =&gt; 'submit',
'#value' =&gt; t('Add one more'),
'#submit' =&gt; array('hook_add_more_add_one'),
// See the examples in ajax_example.module for more details on the
// properties of #ajax.
'#ajax' =&gt; array(
  'callback' =&gt; 'hook_add_more_callback',
  'wrapper' =&gt; 'names-fieldset-wrapper',
),
);

if ($form_state['num_names'] &gt; 1) {
$form['emails']['remove_name'] = array(
  '#type' =&gt; 'submit',
  '#value' =&gt; t('Remove one'),
  '#submit' =&gt; array('hook_add_more_remove_one'),
  '#ajax' =&gt; array(
    'callback' =&gt; 'hook_add_more_callback',
    'wrapper' =&gt; 'names-fieldset-wrapper',
  ),
);
}