I have this script which I use to link to other sites, but I want to make another one of these, but if I copy all this, except the Javascript, the script breakes and the selects don't go to the given urls anymore.
How could I make this work, the best possible way, with multiple select forms?
<form name="event_type_selector" method="post" action="#">
<select name="url_list" class="event-type-selector-dropdown" onchange="gotosite()">
<option value="" selected="selected" disabled="disabled">Vælg venligst...</option>
<optgroup label="Selection 1:">
<option value="?value-now1">Value-Now 1</option>
</optgroup>
<optgroup label="Selection 2:">
<option value="?value-now2">Value-Now 2</option>
</optgroup>
</select>
<script language="javascript">
function gotosite() {
var URL = document.event_type_selector.url_list.options[document.event_type_selector.url_list.selectedIndex].value;
window.location.href = URL;
}
</script>
Dhaval Marthak
17.4k6 gold badges48 silver badges69 bronze badges
asked May 13, 2014 at 13:04
Thomas Nielsen
6132 gold badges15 silver badges32 bronze badges
1 Answer 1
Just pass in a reference to the <select> in the onchange event. That way you don't need to reference the SELECT from the global scope:
<select ... onchange="gotosite(this)">
function gotosite(select) {
window.location.href = select.value;
}
answered May 13, 2014 at 13:09
Greg Burghardt
19.2k10 gold badges59 silver badges106 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js