1

I got an GET request on a webpage, with multidimensional checkboxes. The GET request would look like this:

&company_state[AKERSHUS]=AKERSHUS&company_state[AKERSHUS][]=ASKER&company_state[SOGN+OG+FJORDANE]=SOGN+OG+FJORDANE&company_state[SOGN+OG+FJORDANE][]=ASKVOLL

As you can see, company_state is an array, which again contains an array of values.

Is it possible to use jQuery or plain JS to grab the URL parameters, and trigger a click on the checkboxes with the same name (company_state, company_municipality) and value?

I tried using this, but this doesn't seem to work in this purpose to grab the params.

function getQueryParams(qs) {
  qs = qs.split("+").join(" ");
  var params = {},
    tokens,
    re = /[?&]?([^=]+)=([^&]*)/g;
  while (tokens = re.exec(qs)) {
    params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
  }
  return params;
}

Thanks in advance.

EDIT:enter image description here

Gives this result in console:

enter image description here

1 Answer 1

1

You can parse query string as array, and then iterate over array and extract key and value.

Code is like this:

    function getQueryParameters(str) {
      return (str || document.location.search).replace(/(^\?)/,'').split("&").map(
        function(n){
          return n = n.split("="),this[n[0]] = n[1],this;
        }.bind({}))[0];
  }

var url = "&company_state[AKERSHUS]=AKERSHUS&company_state[AKERSHUS[]=ASKER&company_state[SOGN+OG+FJORDANE]=SOGN+OG+FJORDANE&company_state[SOGN+OG+FJORDANE][]=ASKVOLL";

var arr = getQueryParameters(url);


for(var a in arr){
  console.log("key:" , a.substr('company_state'.length), "   value:" ,  arr[a]);
}

You have here a live example.

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

3 Comments

It doesn't look like it's working like it should. The company_state[value] may also have a array of values. Like so: var url = "&company_state[AKERSHUS]=AKERSHUS&company_state[AKERSHUS[]=ASKER&company_state[SOGN+OG+FJORDANE]=SOGN+OG+FJORDANE&company_state[SOGN+OG+FJORDANE][]=ASKVOLL&company_state[SOGN+OG+FJORDANE][]=EID" As you can see, SOGN+OG+FJORDANE has two child values.
Can you edit your post with an example of the result?
Okay, I fixed it another way using old input data (Laravel) to check the checkboxes.

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.