Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change href link with dropdown value

I got option values from a query, and i added into dropdown list. here's the code:

$(document).ready(function() {
    $( "#uname" ).focusout(function() {
        $("#loader").show();
        var uname = $( "#uname" ).val(),
            v_request = $.ajax({
            url  : "data/get_agent.php",
            type : "POST",
            dataType: "json",
            data : {
                uname  : uname
            }
        });

        v_request.done(function(data, status, jqXHR) {
            $("#application").empty();
            var option = document.createElement("option"),
                select = document.getElementById("application");
                option.text = '-- Application --';
                option.value = '';
                select.appendChild(option);

            $.each(data.data, function(key, data) {
                var option = document.createElement("option"),
                    select = document.getElementById("application");
                option.text = data.appname;
                option.value = data.appvalue;
                select.appendChild(option);
                if(data.defvalue == 1)
                    select.value = data.appvalue;
            });
        });
    })
});

the option value contain url link, i want call it in a a href tag. here's code:

<tr>
    <td>
        <select id="application" name="application">
        <option value=""> -- Application --  </option>
        </select>
    </td>
</tr>
<tr>
    <td align="center" style="padding-top:10px;" id="logInBtn">
    <a id="link_combo" href="javascript:void(0);" onmouseout="MM_swapImgRestore()"><img src="resources/images/straclick-login.png" name="btn-login" border="0" width="280" id="btn-login" /></a>
    </td>
 </tr>

i don't know how to insert drop down value into href.

like image 630
DimasW Avatar asked May 05 '15 05:05

DimasW


1 Answers

Just adding to Barmar's answer, here's an example: http://jsbin.com/panacoqaje/1/ (I would have commented but I don't yet have 50 reputation :P)

$("#application").change(function () {
  console.log(this.value);
  $("#link_combo").attr('href', this.value);
  $("#link_combo").text($("#application option:selected").text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr>
    <td>
        <select id="application">
        <option value="http://google.com/">Google</option>
        <option value="http://benzhang.xyz/">A Blog</option>
        </select>
    </td>
</tr>
<tr>
    <td align="center" style="padding-top:10px;" id="logInBtn">
      A link to: 
    <a id="link_combo" href="#">Unicorn Land</a>
    </td>
 </tr>
like image 84
Ben Avatar answered Sep 18 '22 11:09

Ben