Reputation: 46750
I have this markup
<form name="sortbyformtop">
<select onchange="location.href=sortbyformtop.sortbyselecttop.options[selectedIndex].value" id="sortbyselecttop">
<option value="/Search"></option>
<option value="/Search?sortby=accommodationtype">Accommodation type</option>
...
</select>
</form>
Whenever I select one of the options in the drop down list I get this Javascript error in Firebug:
TypeError: sortbyformtop.sortbyselecttop is undefined
Is this sort of thing possible in some way?
Upvotes: 1
Views: 7244
Reputation: 123377
try with
location.href= document.getElementById('sortbyselecttop').options[document.getElementById('sortbyselecttop').selectedIndex].value;
or shortly
location.href= this.options[this.selectedIndex].value;
since this
in that context is equivalent to document.getElementById('sortbyselecttop');
selectedIndex
needs to be tied to a specific select element (it's a property of the select itself), otherwise you are looking for a global (undefined) variable called selectedIndex
Upvotes: 4
Reputation: 167172
Instead of sortbyformtop.sortbyselecttop
give this
or document.getElementById('sortbyselecttop')
.
document.getElementById('sortbyselecttop').options[document.getElementById('sortbyselecttop').selectedIndex].value
Or
this.options[this.selectedIndex].value
Upvotes: 1
Reputation: 6759
A shorter version:
location.href = this.options[this.selectedIndex].value
Upvotes: 1
Reputation: 13501
Why are you trying to do this in an inline event handler?
I'm sure it is possible to do what you are trying to do - I think you might need a 'name' attribute on your select element though rather than an ID, but it may be easier to have a seperate function give you a bit more flexibility - and then call the function from your event handler instead.
onchange="doSomething();"
Upvotes: 0