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
  • 74,352
  • 26
  • 153
  • 180
Steven
  • 18,761
  • 70
  • 194
  • 296

2 Answers2

4

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
  • 312,895
  • 29
  • 395
  • 388
-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,090
  • 2
  • 45
  • 53