How to add a javascript function after uploading file

Drupal Answers Asked by Ahmed on December 9, 2020

I make a form using FAPI, and this form contains a file managed field. I want to show an alert after uploading the file (“uploaded successfully”), and a confirmation (“Are you sure you want to delete file”) when deleting the file.

This my code.

$form['my_file_fid'] = array(
  '#type' => 'managed_file',
  '#title' => t('Files'),
  '#default_value' => variable_get('my_file_fid', ''),
  '#upload_location' => 'public://',

I tried to add #file_value_callbacks, but I don’t know how to use it.

I tried to make a javascript just to alert when I click upload,remove button but it doen not work I tried to make it general to all input to check that js is correct now all inputs are clickable but upload and remove button not

jQuery(document).ready(function() {
         jQuery('input').bind('click',function() { alert("hi") });

I make this js to check jquery ajaxSuccess function

2 Answers

Hope this will help, use hook_file_presave inside that you can use drupal set message.

 * implementing hook_node_presave 
function mymodule_file_presave($file) {
 if (isset($file->source) && $file->source=='field_preview_image_file_und_0')//make condition like this for ur file
//use the drupal set message or java script alert  on successful upload
drupal_add_js(drupal_get_path('module','mymodule') . '/test.js');

create a js file on same module folder as test.js

(function ($) {
alert('this is alert message');  

Answered by madhurjya on December 9, 2020

You could just bind something to the jQuery ajax success event on the page where your form is displayed.

You can bind ajaxSuccess to any jQuery object, try something like this if you're experimenting:

  (function ($) {
    $(function () {
      jQueryObject = $({});
      jQueryObject.ajaxSuccess(function(event, XMLHttpRequest, ajaxOptions) {
        /* do something whenever an ajax call is successful */

Answered by David Meister on December 9, 2020

Add your own answers!

Related Questions

Social Media Embed Problems With the URL Embed module

0  Asked on November 30, 2021 by mckinzie25


Contextual links for node blocks

3  Asked on November 28, 2021


Set node to flagged by default

1  Asked on November 28, 2021 by andrew-morris


How do I replace drush_set_error()?

1  Asked on November 28, 2021


Get file ID and path to use in View template

1  Asked on November 25, 2021 by srodrig


How to use drupal_add_library with a module?

2  Asked on November 25, 2021


Ajax Form Not Working in some Some Regions

2  Asked on November 23, 2021 by sandesh-yadav


Facebook Album fetcher

1  Asked on November 23, 2021 by rajat-goyal


Condition on current moderation state

2  Asked on November 23, 2021 by paul-cappucci


how to apply a css code for a specific screen size

1  Asked on November 23, 2021 by coding-mv


hook_entity_predelete never called

0  Asked on November 23, 2021


User reference and Views 3 and Panels 3

1  Asked on November 21, 2021


MetaTag with JSON API won’t serialize

2  Asked on November 21, 2021 by johnventions


How to disable Rich Text Editor in image field caption ?

1  Asked on November 19, 2021 by lee-woodman


search block form not accepting special characters in drupal 7

1  Asked on November 19, 2021 by blueztechz


Unknown engine type outputformat

1  Asked on November 19, 2021 by gilles9999


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP