How can I display a Browser Prompt for Confirmation before executing ajax callback for a button element? I have a button type element in a form. It has an ajax callback to some function that performs a delete operation. Everything works perfect.

I want to add a browser prompt when user clicks on the delete button. The prompt should ask user to confirm the delete action and only after the confirmation should the callback function be executed (using ajax of course). 

How can I do this?

Following is the code for button element:

$form['delete'] = array(
  '#type' =&gt; 'button',
  '#value' =&gt; t("Delete"),
  '#element_validate' =&gt; NULL,
  '#ajax' =&gt; array(
    'callback' =&gt; 'delete_something',
    'wrapper' =&gt; 'delete-message',
  ),
  '#limit_validation_errors' =&gt; array(),
);

function delete_something(){
  //Some Code
}
This one should help you the same way you want to achieve the functionality.
Instead of ajax callback I have created a ctools modal which will return the delete and cancel button..

"delete/nojs/1" where 1 will be the Item which you want to delete.

use this in your form.

$form['delete'] = array(
  '#markup' =&gt; ctools_modal_text_button(t('Delete'),"delete/nojs/1", t('Delete'), 'ctools-modal-modal-popup-small'),
);


create a menu item.

function custom_module_menu() {
  $items = array();
  $items['delete/%ctools_js/%'] = array(
    'type' =&gt; MENU_CALLBACK,
    'title' =&gt; t('Delete'),
    'page callback' =&gt; 'delete_something',
    'page arguments' =&gt; array(1,2),
    'access callback' =&gt; TRUE,
  );
  return $items;
}


and call this function.

function delete_something($js = NULL) {
  if (!$js) {
    return drupal_get_form('delete_form');
  }
  ctools_include('modal');
  ctools_include('ajax');
  $form_state = array(
    'title' =&gt; t('Delete'),
    'ajax' =&gt; TRUE,
  );
  $output = ctools_modal_form_wrapper('delete_form', $form_state);
  if (empty($output)) {
    $output[] = ctools_ajax_command_reload();    
  }
  print ajax_render($output);
  drupal_exit();
}

function delete_form($form_state = NULL){
  $form['message'] = array(
    '#prefix' =&gt; '&lt;div class="delete_message"&gt;',
    '#value' =&gt; t('Are you sure want to delete?'),
    '#suffix' =&gt; '&lt;/div&gt;',
  );
  $form['submit']['delete'] = array(
    '#prefix' =&gt; "&lt;div id = 'delete_button'&gt;",
    '#suffix' =&gt; "&lt;/div&gt;",
    '#value' =&gt; t('Delete'),
    '#type' =&gt; 'submit',
    '#submit' =&gt; array('delete_form_submit'),
  );
  $form['submit']['cancel'] = array(
    '#prefix' =&gt; "&lt;div id = 'cancel_button'&gt;",
    '#suffix' =&gt; "&lt;/span&gt;&lt;/div&gt;",
    '#value' =&gt; t('Cancel'),
    '#type' =&gt; 'submit',
  );
  return $form;
}

function delete_form_submit($form, &amp;$form_state) {
 //some code to delete
}