Forum Moderators: open

Message Too Old, No Replies

this.selectedIndex has no properties

         

JAB Creations

5:14 am on Oct 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I remove the original script...
<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

daveVk

10:18 am on Oct 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try

<select onchange=myfunction>

rocknbil

8:30 am on Oct 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks to me like you have two choices. You get the error because if you are referring to this within the function, this refers to . . . what? The function itself?


<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. :-)

daveVk

11:40 am on Oct 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Tell me if I am missing the point

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

JAB Creations

4:07 pm on Oct 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was working on this last night (then fell asleep) and was able to get it to work though it's a little tricky because I use the base element. IE and Opera both do not take the base element in to effect where Gecko does. I've got to keep working on this for Opera and still need to test OSX and Linux browsers. Thanks for your replies so far, here is what I came up with last night...
- John

function mplayerselect() {
if (document.all) {location.href=location.href=document.getElementById('mplayermenu').value;}
else if (document.getElementById) {location.href='mplayer/' + document.getElementById('mplayermenu').value;}
}