0

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.

4
  • So redirect to that URL with the value? Or change the href of that link? Commented Mar 27, 2014 at 11:20
  • use JQuery to get the value change href Commented Mar 27, 2014 at 11:22
  • Instead of alert, document.getElementById('yourlink').href += '?'+ sel.options[sel.selectedIndex].text + "=" + value; Commented Mar 27, 2014 at 11:24
  • Why aren't you just submitting the form with the select element in it? Commented Mar 27, 2014 at 11:27

3 Answers 3

2

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;
};
Sign up to request clarification or add additional context in comments.

Comments

0

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;
});  
}

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.