-1

I'm trying to use the Ajax File Upload as featured here: http://valums.com/ajax-upload/

As you can see, I need to create a qq.FileUploader object to initialize the script. However, I need to be able to dynamically create this objects without knowing the IDs of the elements. I've tried creating something like this:

var uploader, i = 0;
$(".file-upload").each(function() {
    $e = $(this);
    i++;
    uploader[i] = new qq.FileUploader({
        element: $(this)[0],
        action: 'uploadfile.php',
        allowedExtensions: ['doc', 'docx', 'pdf'],
        multiple: false,
        onComplete: function(id, fileName, responseJSON) {
            $($e).siblings('input').val(responseJSON.newfilename);
        }
    });
});

I've learned that the [i] part I have added breaks the script, because I cannot have objects inside of an array.

Is there another way I can create this objects dynamically? They need to all have a unique name, otherwise the onComplete function gets overwritten for all of them. I experimented with using eval(), but I can't seem to make it work correctly.

2
  • 1
    $e = $(this); so you don't need to re-wrap it later. $e.siblings('input').val(responseJSON.newfilename); also $(this)[0] is redundant. just use element: this, also you can remove i++; and just put uploader[++i] = new qq.FileUploader({... scratch that.. you can change $(".file-upload").each(function() { to $(".file-upload").each(function(i) { and leave out the variable i and increment all together. tl;rd Commented Aug 19, 2012 at 17:45
  • here is a demo with uploader.push() for kicks Commented Aug 19, 2012 at 17:53

2 Answers 2

2

You have to declare uploader as an array first :

var uploader = [];  

Because you declared the variable without defining it, it has the default value of undefined , and your code was translated into something like undefined[i] which triggers an error.

Sign up to request clarification or add additional context in comments.

7 Comments

i feel like assigning something to the array in this fashion is a strange idea, would rather use uploader.push()
what exactly do you mean, @rlemon?
You are advocating using Array.push; yet your answer assigns the variable as a object.
@rlemon because both options are valid. Either [] and uploader.push(), or {} and uploader.[i] = ....
|
1

Has to be something like

var uploader = {}; 

or else uploader is null and you cannot assign anything to it.

EDIT:

So there're two opitions, in my opinion, if one wants to have an array than it makes sense to declare one, var uploader = []; and then use the uploader.push() method or define it as an object var uploader = {}; and just do uploader[i] = ....

It is also possible to do the latter with an a array, but in the latter case I see no point in maintaining the counter (i).

7 Comments

I didn't downvote, because this answer is not really wrong, but it's more likely that the OP wants a real array and not just an object as the indexes are numeric. Thus, var uploader = []; is probably a better answer.
@Pointy I agree, but then array.push() sounds more appropriate to me, as I already pointed out above.
TypeError: Object #<Object> has no method 'push'
@rlemon, that's my point exactly, if you declare something to be an array, than it makes sense to use array-specific functions such as push(). If you want to just do something[i] = 'whatever', than it's fine to just have an object.
@Qnan well except that an array will automatically maintain the .length property regardless of how properties (with numeric "names") are added.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.