ghaschel
ghaschel

Reputation: 1355

Compose a link based in select's option value

I have 2 dropdown menus, and I need to compose a link with it's values.

Here is the code:

<form id="dropdown1">
    <select id="linha">
        <option value="G12">Option 1</option>
        <option value="G11">Option 2</option>
        <option value="H89">Option 3</option>
    </select>
    <select id="dia">
        <option value="all">Every day</option>
        <option value="work">working days</option>
        <option value="sat">saturday</option>
        <option value="sun">sunday</option>
    </select>
</form>

I need something in JavaScript to "compose" a link with http://somewebsite.com/*selected_linha_value*/*selected_dia_value*

How can I do that?

Upvotes: 1

Views: 6076

Answers (5)

ChatGPT
ChatGPT

Reputation: 5617

HTML

<div class="container">
    <select class="small-nav">
    <option value="" selected="selected">Go To</option> 
    <option value="http://whiterabbitexpress.com">Services</option>
    <option value="http://shop.whiterabbitjapan.com">Shop</option>
</div><!-- container -->

JScript:

$(".small-nav").change(function() {
        window.location = $(this).find("option:selected").val();
    });

Upvotes: 1

vivek salve
vivek salve

Reputation: 991

    <select name="dia" id="dia">
      <option value="all">Every day</option>
      <option value="http://stackoverflow.com">working days</option>
      <option value="http://anotherSite.com">saturday</option>
      <option value="http://anotherSite2.com">sunday</option>
    </select>
    <script>
    $("#dia").change(function () {
              var selctedValue = "";
              $("select option:selected").each(function () {
                    selctedValue += $(this).val();
window.location.href = selctedValue;

    });
    });

Upvotes: 4

Stano
Stano

Reputation: 8949

What about this one?

function make_url(){
    var linha = document.getElementById('linha').value;
    var dia = document.getElementById('dia').value;
    var url=window.location.href;
    var pos=url.indexOf('?');
    if (pos>-1){
        url = url.substr(0,pos);
    }
    //alert(url + '?linha='+linha+'&dia='+dia); return;
    document.location.href = url + '?linha='+linha+'&dia='+dia;
}

fiddle

Upvotes: 2

Praveen Singh
Praveen Singh

Reputation: 542

i think u need something like this.

<script type="text/javascript">
params = getParams();
var name1 = unescape(params["linha"]);
switch(name1)
{
case "g12":
window.location = "http://www.google.com"
}

function getParams(){
var idx = document.URL.indexOf('?');

var params = new Array();
if (idx != -1) {
var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
for (var i=0; i<pairs.length; i++){
nameVal = pairs[i].split('=');
params[nameVal[0]] = nameVal[1];
}
}
return params;
}

Its not the full code. It will give you some idea. If you have any doubt just comment

Upvotes: 3

smilly92
smilly92

Reputation: 2443

Take a look at: http://jsfiddle.net/ERHhA/

You can use jQuery val() to get the value of the select boxes. Then just append these values to the base url.

var url = "http://somewebsite.com/" + $('#linha').val() + "/" + $('#dia').val();

Upvotes: 2

Related Questions