PDA

View Full Version : Selecting dropdown value using link.



remp
06-26-2012, 04:49 AM
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.

CoursesWeb
06-26-2012, 07:00 AM
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:

<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>