Forum Moderators: open
<select onchange="location=this.options[this.selectedIndex].value">
<option value="example.php">option example</option>
</select>
...and replace it with a function I receive the error this.selectedIndex has no properties.
<select onchange="myfunction();">
<option value="example.php">option example</option>
</select>
How do I amend the script to work properly when the function is called from an external file? ...yes I know this is not completely unobtrusive, I'll get to that soon enough. ;)
- John
<select onchange="myfunction([b]this[/b]);">
<option value="">select</option>
<option value="example.php">option example</option>
</select>
<script type="text/javascript">
function myfunction ([b]selectObject[/b]) {
var ind = selectObject.options.selectedIndex;
// allows reset
if (ind>0) { document.location = selectObject.options[ind].value; }
}
</script>
Better yet:
<script type="text/javascript">
function myfunction () {
// This can be one statement but it's easier to read this way:
var ind = document.getElementById('myselect').selectedIndex;
if (ind>0) { document.location=document.getElementById('myselect').options[ind].value; }
}
window.onload=function () {
if (document.getElementById('myselect')) {
document.getElementById('myselect').onchange=function() { myfunction(); }
}
};
</script>
</head>
<body>
<form>
<select id="myselect">
<option value="">select</option>
<option value="example.php">option example</option>
</select>
</form>
The blank at the top of the list allows it to be reset if the back button is used. I'd put a "go" button next to it too though. :-)
Original code
onchange="location=this.options[this.selectedIndex].value"
Short for
onchange=function{location=this.options[this.selectedIndex].value;}
Replace Anon function by named function
onchange=myfunction; // may not work early browsers
function myfunction(){location=this.options[this.selectedIndex].value;}
function mplayerselect() {
if (document.all) {location.href=location.href=document.getElementById('mplayermenu').value;}
else if (document.getElementById) {location.href='mplayer/' + document.getElementById('mplayermenu').value;}
}