Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
JQuery/Javascript: Build URL string from multiple selects

I have the following selects:

<select name="wpf2750_20">
  <option value="200">AAA</option>
  <option value="400">BBB</option>
</select>
<select name="wpf2750_27">
  <option value="600">CCC</option>
  <option value="800">DDD</option>
</select>

And then the following script:

<script>
  jQuery('select').on('change', function() {
    var url = 'http://example.com/file.html' + '?' + this.name + '=' + this.value;
    alert(url);
  });
</script>

This gives me the correct URL with the name of the select once changed as well as the value. As there are multiple selects how do I build a URL with the complete string?

Currently it gives me: http://example.com/file.html?wpf2750_20=AAA

What I want to give me is: http://example.com/file.html?wpf2750_20=AAA?wpf2750_27=CCC

So with each select it builds on the URL. If they change the option then the URL also needs to change to reflect this.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Collect all the names and values into an object. Construct your string by indexing into that object.

const $selects = $('select');
$selects.on('change', event => {
  const fragments = Object.fromEntries(
    $selects.map((i,e) => ([e.name, e.value])).get()
  );
  const url = `http://example.com/file.html?${fragments['wpf2750_20']}=${fragments['wpf2750_27']}`;
  console.log(url);
});

Or, more generally, build a query string from the selects and then append it to the url prefix:

const $selects = $('select');
$selects.on('change', event => {
  const query = $selects.map((i,e) => `${e.name}=${e.value}`).get().join('&');
  const url = `http://example.com/file.html?${query}`;
  console.log(url);
});

References:

  • Template literals
  • Object.fromEntries()
  • jQuery's .map()
  • jQuery's .get()
about 4 years ago · Juan Pablo Isaza Report

0

It is because your event only triggered when you change the input, and your code only capture the element that triggered event.

If you want to capture every select element, then instead of use this, you need to select every select element when the select input has changed.

You can try this

<script>
  $('select').on('change', function () {
      let params = [] 
      $('select').each((i,e)=>{
          params.push(`${$(e).prop("name")}=${$(e).val()}`)
      })
      var url = 'http://example.com/file.html' + '?' + params.join("&"); //suppose params on url join with & symbol but you are joining with ?

      alert(url);
  });
</script>

Replace the $ with your jQuery

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!