Nilesh
Nilesh

Reputation: 113

How to get select box value in a href tag

How do I get the select box value in a href tag with php variable?

<script type="text/javascript">
$(document).ready(function(){
        $("select.id").change(function(){
            var selectedid  = $(".id option:selected").val();
            alert(selectedid);
        });
    });
</script>

<label>Select Id</label>
<select class="id">
    <option value="">SELECT</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="4">5</option>
</select>

<a href="test.php?id=id">Testing</a>

Upvotes: 1

Views: 3404

Answers (2)

Unni Babu
Unni Babu

Reputation: 1814

Code shown below, tested and works 100%, use jQuery attr and give an ID to hyperlink, here the id is "link":

<a id="link" href="test.php?id=id">Testing</a>  <!-- GIVE AN ID, else will affect entire hyperlinks on your project -->

$(document).ready(function(){
        $("select.id").change(function(){

            var selectedid  = $(".id option:selected").val();
            $("#link").attr("href","test.php?id="+selectedid);  //-----this will change href 
        });
    });

Upvotes: 2

Shailendra Sharma
Shailendra Sharma

Reputation: 6992

by using .attr() you can change href of <a> tag

change href on change of select option

$(document).ready(function(){
        $("select.id").change(function(){
            var selectedid  = $(".id option:selected").val();
            $('a').attr('href','test.php?id='+selectedid);
        });
    });

Working Fiddle : http://jsfiddle.net/ehxj9zrx/

Upvotes: 0

Related Questions