Question

I have several dropdown boxes on a site and I need to append/add/remove query string depending on what the user has chosen. How can I accomplish it? Here is some generic code for demo purposes:

<select name="status" onchange="document.getElementById('Iframe2').src = document.getElementById('Iframe2').src + this.options[this.selectedIndex].value">
<option></option>
<option value="&A=1">1</option>
<option value="&A=2">2</option>
</select>


<select name="plan" onchange="document.getElementById('Iframe2').src = document.getElementById('Iframe2').src + this.options[this.selectedIndex].value">
<option></option>
<option value="&FilterGarage=Right">Right</option>
<option value="&FilterGarage=Left">Left</option>
</select>

So what I would like to happen is let's say the user selects the "1" option and then the "Right" option. The way I have it now, it works fine. The problem happens when the user then goes back and changes the "Right" option to "Left". At that point what's happening is it's just adding another &FilterGarage parameter and ends up not showing anything.

I guess what I need to know is how to remove the option value and regenerate it depending on what the user picks each time.

Was it helpful?

Solution

You can achieve this by pure JavaScript. Just add id to your <select> elements.

<select name="status" id="status">

and

<select name="plan" id="plan">

And add the JavaScript at the end of HTML <body>.

<script type="text/javascript">
function set_url(){
    status = document.getElementById("status").value;
    plan = document.getElementById("plan").value;
    s = url + status + plan;
    //alert(s); 
    document.getElementById("Iframe2").src = s;
}
url = "http://www.google.com/search?q=JavaScript";
document.getElementById("status").onchange = set_url;
document.getElementById("plan").onchange = set_url; 
</script>

NOTE: The base URL should be stored in a variable; like url in above example.

OTHER TIPS

capture combo's change event, then you change link href depending on the selected value. The first time, you save the base_href, to avoid to compute it each time. Everytime you save the changed values to a var and add the two variables to the url.

var base_href="",status="",plan="";
$('#plan').on('change', function() {
   plan=$(this).val();
    if (base_href=="")
        base_href = $("#Iframe2").attr("src");
   $("#Iframe2").attr("src",base_href+plan+status);
 });
$('#status').on('change', function() {
   status=$(this).val();
    if (base_href=="")
        base_href = $("#Iframe2").attr("src");
   $("#Iframe2").attr("src",base_href+plan+status);
 });

I am doing it with jquery(im writing with my mobile and it less code to write) but if you dont want to use jquery the only thin that changes is the name of the functions. The idea is the same!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top