Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This question already has an answer here:

I have a form that uses the get method and contains an array:

http://www.example.com?name[]=hello&name[]=world

I'm trying to retrieve array values 'hello' and 'world' using JavaScript or jQuery.

I've had a look at similar solutions on Stack Overflow (e.g. How can I get query string values?) but they seem to only deal with parameters rather than arrays.

Is it possible to get array values?

share|improve this question

marked as duplicate by Kinlan, Rob Mensching, Cheran Shunmugavel, Signare, Toon Krijthe Apr 8 '13 at 5:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
The answer stackoverflow.com/a/3855394/295783 in the link you posted will work in your case too, unless the parms repeat as suggested by you mentioning array and the use of [] – mplungjan Apr 7 '13 at 17:56
1  
Just to be clear: The URL does not have the concepts of arrays. All you have are multiple parameters with the same name. The [] are typically used for PHP servers, which will then created arrays out of these parameters. Other languages (e.g. Python) can handle multiple parameters with the same name (and without []) just fine. – Felix Kling Apr 7 '13 at 17:56
2  
From the question you linked to, you might find this answer helpful: stackoverflow.com/a/9362596/218196. – Felix Kling Apr 7 '13 at 18:00
    
Okay thanks guys. Apologies I thought it was an array. 'name' is a checkbox form so multiple values are sent with the same name. – iltdev Apr 7 '13 at 18:05
up vote 8 down vote accepted

There you go: http://jsfiddle.net/mm6Bt/1/

function getURLParam(key,target){
        var values = [];
        if(!target){
            target = location.href;
        }

        key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");

        var pattern = key + '=([^&#]+)';
        var o_reg = new RegExp(pattern,'ig');
        while(true){
            var matches = o_reg.exec(target);
            if(matches && matches[1]){
                values.push(matches[1]);
            }
            else{
                break;
            }
        }

        if(!values.length){
             return null;   
         }
        else{
           return values.length == 1 ? values[0] : values;
        }

    }

    var str = 'http://www.example.com?name[]=hello&name[]=world&var1=stam';

    console.log(getURLParam('name[]',str));
    console.log(getURLParam('var1',str));
share|improve this answer
    
Thanks so much, Adidi. That is perfect! :) – iltdev Apr 7 '13 at 18:19
    
You are very welcome ^^ – Adidi Apr 7 '13 at 18:20
    
After a lot of struggling, I figured out that we need to append [] to name if it is an array! looks obvious now, but somehow I wasted an hour to notice it. – Atul Vaibhav Jul 3 '16 at 17:08
    
Also, I replaced 4th line with: "target = decodeURIComponent(location.search);" to get this function to work. – Atul Vaibhav Jul 3 '16 at 17:12
    
is there a way to return whole query string as object instead of passing params? – bonez Sep 22 '16 at 13:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.