Results 1 to 2 of 2

Thread: Selecting dropdown value using link.

  1. #1
    Join Date
    Apr 2007
    Posts
    149
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Selecting dropdown value using link.

    Is there a way to use a link to set the value of a dropdown on the incoming page?

    For example: if i click http://www.mysite.com/incomingpage.php#value1
    then i would have such value already selected in the dropdown.

    All your help is greatly appreciated.

  2. #2
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Hi,
    You need a JavaScript code that gets the hash (value1) from url, get the select list, traverse the option and set "selected" to the option with value from hash.
    Like in this script:
    Code:
    <select name="sl1" id="sl1">
     <option value="value0">val 0</option>
     <option value="value1">val 1</option>
     <option value="value2">val 2</option>
    </select>
    
    <script type="text/javascript"><!--
    // http://www.coursesweb.net
    var val = window.location.hash.replace('#', '');
    var opts = document.getElementById('sl1').getElementsByTagName('option');
    for(var i=0; i<opts.length; i++) {
      if(opts[i].value == val) {
        opts[i].selected = true;
        break;
      }
    }
    --></script>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •