Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Is there a right way to run javascript confirm() when Remove button is pressed ?
I tried the following but it won't bind the click event. Nothing happens.
Drupal.behaviors.mymoduleConfirmRemoval = function(context) {
$('input.form-submit', context).bind('click', myconfirmFunc );
};
It, however, does bind with mousedown event but mousedown ignores my_confirm()'s return value.
Any insight would be appreciated.
Comments
Comment #1
drudrew CreditAttribution: drudrew commentedComment #2
drudrew CreditAttribution: drudrew commentedComment #3
iantresman CreditAttribution: iantresman commentedYes please, I've lost count of the number of times I've pressed the Remove button, thinking it is a Save button. When I have an image, Description field, Title Field, and extended Caption field, it is a lot of text (and image) to lose in a single click.
Comment #4
quicksketchHi guys, I probably won't be adding this directly to FileField, since this module is no longer receiving features. It should be possible to implement via an add-on module.
As @drudrew noted in the original posting, you can bind an additional handler to the mousedown() event. Then you can use http://api.jquery.com/event.stopImmediatePropagation/ to prevent the triggering of further handlers.
Comment #5
myhemant CreditAttribution: myhemant commentedHi,
Please try with this, Its working fine for me.
Create a copy of misc/ajax.js file and put it any custom module and by drupal_add_js() function.
Now modify the
Drupal.ajax = function (base, element, element_settings) { .... // function in beforeSend:
beforeSend: function (xmlhttprequest, options) {
if( ajax.url =='/?q=field_collection/ajax'){ // condition for when click on the Remove button for collection filed module.
if (confirm('Are you sure you want to remove this?')) {
ajax.ajaxing = true;
return ajax.beforeSend(xmlhttprequest, options);
}
xmlhttprequest.abort();
} else{
ajax.ajaxing = true;
return ajax.beforeSend(`xmlhttprequest`, options);
}
},
Comment #6
pwolanin CreditAttribution: pwolanin as a volunteer and at Acquia commented