Nirali Joshi
Nirali Joshi

Reputation: 2008

javascript variable as a get parameter

I am having one select box. i want to pass its selected value with link as a get parameter. i am getting select box's value in javascript on onchange event of select box. now how can i pass with link??

<script type="text/javascript">
    function getArticle(sel){
       var value = sel.options[sel.selectedIndex].value;  
       alert(value);
   }
</script>
 <a href="edittoc.php?key=value">Edit</a> // how can i do this?

Please help me out.

Upvotes: 0

Views: 50

Answers (3)

Tuhin
Tuhin

Reputation: 3373

Try this way>

<script type="text/javascript">
        function getArticle(sel){
           var value = sel.options[sel.selectedIndex].value;  
           var link=document.getElementById("link").getAttribute("href");
           document.getElementById("link").setAttribute("href",link+value);
       }
    </script>
     <a id="link" href="edittoc.php?key=">Edit</a>

Upvotes: 0

Jeremy Danyow
Jeremy Danyow

Reputation: 26406

here's a couple of options. jsfiddle: http://jsfiddle.net/a7Rc6/

html:

<a id="one-way">Edit</a>

<a id="another-way" href="#">Edit</a>

javascript:

var value = sel.options[sel.selectedIndex].value; 

// one way:
document.getElementById('one-way').href = 'edittoc.php?key=' + value;

// another way:
document.getElementById('another-way').onclick = function () {
    window.location = 'edittoc.php?key=' + value;
};

Upvotes: 2

radia
radia

Reputation: 1486

Here's an example of href edit, it covers cases where you already have parameters in your link:

function getArticle(sel){
   var value = sel.options[sel.selectedIndex].value;
   $('a[href]').attr('href', function(index, href) {
    var param = "key"=value;

    if (href.charAt(href.length - 1) === '?')
        return href + param;
    else if (href.indexOf('?') > 0)
        return href + '&' + param;
    else
        return href + '?' + param;
});  
}

Upvotes: 0

Related Questions