157

Here's a snippet of the start of my code:

var myUpload = $("#upload_link").upload({bla bla bla

Basically what I'm trying to do is make the same call with a few different ID's...

I would have assumed this would work but it doesn't:

var myUpload = $("#upload_link,#upload_link2,#upload_link3").upload({

Any ideas?

hexacyanide
  • 88,222
  • 31
  • 159
  • 162
Webby
  • 2,655
  • 5
  • 26
  • 34
  • What's the upload plugin you are using? Are you sure it follows the recommended plugin development pattern? – bfavaretto Aug 16 '11 at 13:36
  • It should work, must be something with the `upload` function. http://jsfiddle.net/X7TAX/ – Kokos Aug 16 '11 at 13:37

7 Answers7

291

Try this:

$("#upload_link,#upload_link2,#upload_link3").each(function(){
    $(this).upload({
        //whateveryouwant
    });
});
44

If you give each of these instances a class you can use

$('.yourClass').upload()
tonycoupland
  • 4,127
  • 1
  • 28
  • 27
36

You can use multiple id's the way you wrote:

$('#upload_link, #upload_link2, #upload_link3')

However, that doesn't mean that those ids exist within the DOM when you've executed your code. It also doesn't mean that upload is a legitimate function. It also doesn't mean that upload has been built in a way that allows for multiple elements in a selection.

upload is a custom jQuery plugin, so you'll have to show what's going on with upload for us to be able to help you.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
15

Make sure upload plugin implements this.each in it so that it will execute the logic for all the matching elements. It should ideally work

$("#upload_link,#upload_link2,#upload_link3").upload(function(){ });
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
6

If all your elements starting with upload_ in its id have the same purpose or syntax you could try and use the following:

$("*[id^='upload_']").each(function() {
    $(this).upload()
});

This way you don't have to specify every single element in the selector.

Thomas Scheffer
  • 524
  • 4
  • 7
5

it should. Typically that's how you do multiple selectors. Otherwise it may not like you trying to assign the return values of three uploads to the same var.

I would suggest using .each or maybe push the returns to an array rather than assigning them to that value.

Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
1

That should work, you may need a space after the commas.

Also, the function you call afterwards must support an array of objects, and not just a singleton object.

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254