0

I want to set a value for dropdown list by clicking a link, i tried something like this, but it isn't working :

<html>
 <body>
    <select id="select">
       <option value="one">one</option>
       <option value="two">Two</option>
       <option value="three">Three</option>
    </select><br />

  <a class="link1" value="two" href="page.php?cc=two">Two</a><br /><br />
  <a class="link1" value="three" href="page.php?cc=three">Three</a>

  <script> 
       function setSelect () {

           var elmnt = document.getElementsByClassName('link1');

             for (i = 0; i < elmnt.length; i++) {
                    elmnt[i].onclick = function () {
                    document.getElementById('select').value = elmnt[i].getAttribute("value");
                    window.history.pushState('Form', 'My form', this.getAttribute("href"));
                    return false;

                    };                  
            }
       }


       setSelect ();

    </script>
 </body>
 </html>  

I also tried getting a value from the URL into the dropdown list by clicking the link, it didn't work, so i'm trying to set the drop downlist with the link value.
Any help would be much appreciated.

Samwise
  • 111
  • 1
  • 9
  • `for (i = 0; i < elmnt.length; i++) { elmnt[i].onclick = (function(i) { return function() { document.getElementById('select').value = elmnt[i].getAttribute("value"); window.history.pushState('Form', 'My form', this.getAttribute("href")); return false; }; })(i) }` – Rayon Mar 23 '16 at 12:50
  • Do you think you can help me achieve the same thing but with a value from the `href` ? as i have in this [Question](http://stackoverflow.com/questions/36132470/how-to-set-a-value-for-a-dropdown-list-with-a-value-from-url-by-clicking-on-a-li) – Samwise Mar 23 '16 at 13:01

1 Answers1

0

issue in your javascript code please add this

<script>
    function setSelect() {

        var elmnt = document.getElementsByClassName('link1');

        for (i = 0; i < elmnt.length; i++) {
            elmnt[i].onclick = (function (i) {
                return function () {
                    document.getElementById('select').value = elmnt[i].getAttribute("value");
                    window.history.pushState('Form', 'My form', this.getAttribute("href"));
                    return false;
                };
            })(i)
        }
    }
    setSelect();

</script>
Kapil
  • 1,143
  • 13
  • 25
  • Thank You very much @kapil. Do you think you can help me achieve the same thing but with a value from the `href` ? as i have in this [Question](http://stackoverflow.com/questions/36132470/how-to-set-a-value-for-a-dropdown-list-with-a-value-from-url-by-clicking-on-a-li) – Samwise Mar 23 '16 at 13:00