0

what i try to do is the following.

Someone is sending a form via GET to my result page. When the result page is submitted, the URL looks like this for example

index.php?Reiseziel=Italy

Now, theres a select with the id #filtercountry and it contains all the countrys available to filter. When the GET value Reiseziel is set, i want to iterate trough the values of the select and make the right option selected.

For example, the select contains

<select name="Reiseziel" id="filtercountry">
<option value="Please choose..." />
<option value="Germany" />
<option value="Italy" />
<option value="Spain" />

The URL contains "index.php?Reiseziel=Italy" so i want to set the option value "italy" as selected. How would i achieve this?

Dreshar
  • 117
  • 11

5 Answers5

0

check out this topic: Selecting option by text content with jQuery

I belive this does exactly what you're asking.

Community
  • 1
  • 1
Jochen van Wylick
  • 5,303
  • 4
  • 42
  • 64
0

Use this function described here:

function GetURLParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}​

You can then use it like this:

var reiseziel= GetURLParameter('Reiseziel');

And then you set the selected value using the variable.

robasta
  • 4,621
  • 5
  • 35
  • 53
0

Use this function to fetch the value from query string, it uses RegExp:

function getParameter(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

and then to set the value of dropdown like this:

$('#filtercountry').val(getParameter('Reiseziel'));

or this:

var reiseziel = getParameter('Reiseziel');
$('#filtercountry').val(reiseziel);
Raman
  • 1,336
  • 10
  • 13
0
 function QueryString(key) {
                fullQs = window.location.search.substring(1);
                qsParamsArray = fullQs.split('&');
                for (i = 0; i < qsParamsArray.length; i++) {
                    strKey = qsParamsArray[i].split("=");
                    if (strKey[0] == key) { return strKey[1]; }
                }
            }

var Reisezielnew = QueryString("Reiseziel");

You will get country name in Reisezielnew and you can use it as u want.

Somnath Kharat
  • 3,570
  • 2
  • 27
  • 51
0
function getQueryStringFromUrl()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

 $(document).ready(function () {
        var name = getQueryStringFromUrl("Reiseziel");
        $('#dropdownid').val(name);
    });
Anna.P
  • 903
  • 1
  • 6
  • 17