What is the best way to add a confirmation tooltip to the remove button of a managed_file field?

Another Update

I’m trying to implement the solution found here: Intercept click event on a button, ask for confirmation, then proceed, but the remove button still submits normally. I know my JS is targeting the correct element, because I can change other attributes on it like the background color.

Here’s my form:

function form_experiments_01() {   $form['#attached']['js'] = array(     drupal_get_path('module', 'form_experiments') . '/js/form_experiments.js',   );    $form['managed_files'] = array(     '#type' => 'fieldset',     '#title' => t('Managed Files'),   );    $num_docs = 3;   for ($i = 1; $i <= $num_docs; $i++) {     $form['managed_files'][$i] = array(       '#type' => 'managed_file',       '#title' => 'Managed File ' . $i,     );   }    $form['submit'] = array(     '#type' => 'submit',     '#value' => 'Submit',   );    return $form; } 

Here’s my javascript:

(function ($) {   Drupal.behaviors.managedFileAutoUpload = {     attach: function(context, settings) {       $('.form-item input.form-submit[value=Upload]', context).hide();       $('.form-item input.form-file', context).change(function() {         $parent = $(this).closest('.form-item');          //setTimeout to allow for validation         //would prefer an event, but there isn't one         setTimeout(function() {           if(!$('.error', $parent).length) {             $('input.form-submit[value=Upload]', $parent).mousedown();           }         }, 100);       });     }   };    Drupal.behaviors.managedFileConfirmRemove = {     attach: function(context, settings) {       var myButton = $('.form-item input.form-submit[value=Remove]', context);        if (myButton.length > 0) {         var myClick = null;          //get a list of jQuery handlers bound to the click event         var jQueryHandlers = myButton.data('events').click;          console.log(jQueryHandlers);          //grab the first jquery function bound to this event         $.each(jQueryHandlers,function(i,f) {           myClick = f.handler;           return false;         });          //unbind the original         myButton.unbind('click');          //bind the modified one         myButton.click(function(){           if(window.confirm("Are You Sure?")){             myClick();           } else {             return false;           }         });       }     }   } })(jQuery); 

Update

After some more research I found this question. This is what I was trying to ask but didn’t know how to word.

Intercept click event on a button, ask for confirmation, then proceed

I’ll implement this solution and post a resolution once I’ve got it working.

Description

I would like to add a confirmation tooltip when the “remove” button is clicked on my managed_file field.

I would like to do something similar to this:

Bootstrap Confirmation Example

What is the best way to do that?

Possible Solution?

So far, I’m thinking I could probably use javascript to hide the remove button and instead display my own button. Then when my button is clicked, I can trigger a popup that contains a second custom button to cancel and the remove button that I originally hid.

I’m worried that this solution is a bit messy. Is there a more elegant way to do this? Can I get a similar behavior without hiding the original remove button?

Similar Questions

I’m not sure if these apply to my use-case since I’m working with a managed_file field. So far, I’ve been unable to implement similar solutions without the default behavior of the remove button interfering.

Resources

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

What is the best way to add a confirmation tooltip to the remove button of a managed_file field?

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.