Hello,

I am trying to write a small add-on for my custom CCK form (i am using filefield to upload files).

How can I trigger a JS function after the file has been uploaded. ( I want to put a scaled version of the image after that CCK widget and do some other stuff to it, also I can't use imageField because my files can be photoshop files and etc.)

So after file is uploaded (name, size and type shown in the widget) I want to call a function in my custom JS file, I already have my custom JS file loaded.

Some code:

if ($(".filefield-file img").length > 0 ) {
	callme();
	
}

So something like that with jQuery, thing is that code is only loaded when the form is loaded, not when my widget is updated.

Can someone help me please?

Comments

quicksketch’s picture

Category: feature » support
Status: Active » Closed (fixed)

You can do this if you're running jQuery 1.3 or higher by using .live():

$(".filefield-file img").live(function() {
  callme();
});

Note however that Drupal 6 only comes with jQuery 1.2, so you'd need to use jQuery Update module to take advantage of this method.

I'm closing this issue directly due to its age, please reopen if you have further questions on this issue.

kobnim’s picture

Version: 6.x-3.0 » 6.x-3.9
Status: Closed (fixed) » Active

I have a similar need. I would like to trigger a JS function when a file has been uploaded.

I was able to get a simple 'live' example to work:

$("p").live("click", function(){
      callme();
 });

However, I was not able to get this code to work:

$(".filefield-file img").live(function() {
  callme();
});

Is it missing some event, that effectively says "an image has just been uploaded"? And if so, do you know what that event should be?

$(".filefield-file img").live(someEvent, function() {
  callme();
});

And a second question ...
I would like to trigger a JS event when the user has removed a file. Do you know how I would do that?

Thanks very much for your help.

quicksketch’s picture

Status: Active » Closed (won't fix)

I'm unable to provide programming help in the issue queues.