I am trying to add an own JS function to a Drupal Core Form - think Config, Development, Clean Cache. This function basically just asks if the user is sure. Here is what I have so far:

Drupal.behaviors.confirm_delete = {

    attach: function (context, settings) {

         jQuery("#field-module-subsections-values input[id$='-remove-button']", context).bind('mousedown', function (ev) {


            var followLink = window.confirm(question);

            if (followLink) {

            return false;


The problem I have is, that the function gets called before the click-event (thanks to mousedown), but the Ajax action on the button still fires. Maybe someone has a hint as to why?

1 Answer 1


Using hook_form_alter you can implement simple popups like this using #attributes.

 $form['submit']['#attributes'] => array(
  'onclick' => 'if(!confirm("Are you sure?")){return false;}',


$form['submit']['#attributes']['onclick'] = 'if(!confirm("Are you sure?")){return false;}';
  • Thanks for your answer. But can I assign it to one certain button instead of the whole form? Jan 8, 2015 at 8:50
  • Yes, just replace the 'submit' with your button.
    – sareed
    Jan 8, 2015 at 15:03

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.