views:

257

answers:

5

I have a page that loads an HTML form via a $.ajax request.

The form has a file upload option. I have tried several Ajax upload plugins and they all require me to instantiate some ajaxUpload() type object which internally creates the click listener. However these listeners dont trigger because the dynamically loaded form is not DOM accessible.

To get around such things in the past I have used live() to listen. But I cant declare these ajaxUpload instances as a live event. So how can I get this upload button to function?

+1  A: 

I seem to recall, from somewhere, that the livequery plugin can handle this type of thing. In my quest for the truth, I came across this:

http://stackoverflow.com/questions/1515734/issue-with-binding-in-jquery-for-copied-elements

karim79
livequery does not work in jquery 1.4 which I need. thanks though. I am experimenting with live() now.
Jono
A: 

Could you add the dynamically loaded form to the document? (then instantiate the ajaxUpload object ''after'' the form is added to the document)

orip
I am doing exactly that and it does not work. This is because when I add the form, it does not attach to the DOM. So even if I declare the object after it is loaded, it cannot parse that section of the DOM.
Jono
+1  A: 

AJAX cannot upload file. jQuery form plugins using hidden iframe technique to upload the file. To do real AJAX upload, you can use Flash based uploader. I recomended you to try uploadify.

Donny Kurnia
AJAX can upload in an iframe which is fine for my purposes. I have tried uploadify and swfupload and they always give me problems. And I would rather not deal with flash anyway.
Jono
+1 Uploadify is fantastic for a project I'm doing! ty :)
balexandre
A: 

Here is the best I have been able to do so far:

$('#uploadButton').live('click', function(){                
    new AjaxUpload('uploadButton', {
       action: 'upload-handler.php',
       onComplete: function(file, response){
        alert(response);
       });
});

live() works, but it only instantiates the object after the click. So I need to click it again in order for the upload trigger to work.

So I have been trying to remove the extra click somehow. I was hoping the following would work at the end of the function, but it has not:

$(this).trigger("click");
Jono
See my answer. Why not do the above not in a click handler, but in the callback after the ajax request for the form completes?
lawrence
A: 

What do you mean the form is not accessible in the DOM once you fetch it with ajax? Why shouldn't it be?

$.get(url_that_has_form, null, function(resp) {
    $('#some_container').html(resp);
    // Now the form is part of the document.
    // Run your ajax upload stuff here.
    new AjaxUpload('#id_of_form_you_just_added', options);
}, 'html');
lawrence
This will not work. Loading HTML does not add it to the DOM in jQuery. This is the purpose of live().
Jono
You're just wrong about this. Loading HTML does not *automatically* add it to the DOM; that's why I'm *explicitly* adding it with the .html() call. You could also use append() or any of the other related methods. The purpose of live() is so you can set up event handlers *before* fetching.
lawrence