0

I have a url like this:

http://example.com/mypage?vendorId=1&vendorId=2&vendorId=3

Using jQuery, how can I get every vendorId value and put them in an array?

Matt
  • 72,564
  • 26
  • 147
  • 178
Steven
  • 17,851
  • 66
  • 182
  • 285

2 Answers2

3

In javascript, that's just a string:

var url = 'http://mysite.com/mypage?vendorId=1&vendorId=2&vendorId=3'
var qs = url.split('?')[1];
var parts = qs.split('&');
var arr = [];

$.each(parts, function() {
    var val = this.split('=')[1];
    arr.push(val);
});

FIDDLE

or the short way:

var arr = $.map(window.location.split('?')[1].split('&'), function(e,i) { 
    return e.split('=')[1];
});
adeneo
  • 303,455
  • 27
  • 380
  • 377
-1

You don't need jQuery for this.

var pairs=location.search.slice(1).split('&');
var vendors=[];
for (i in pairs){
    bits=pairs[i].split('=');
    if (bits[0]=='vendorId') vendors.push(bits[1]);
}
Popnoodles
  • 28,388
  • 2
  • 42
  • 53