Reputation: 43
<select id="select" onchange="this.options[this.selectedIndex].value
&& (window.location = this.options[this.selectedIndex].value);">
<option value="">Choose your state</option>
<option value="http://www.treasury.state.al.us/Content/index.aspx">Alabama</option>
<option value="http://treasury.dor.alaska.gov/">Alaska</option>
I've tried looking at other examples, but I'm not sure where to add the "_blank" target attribute to this code to let the link open in a new window/tab. Any help would be appreciated.
Thank you!
Upvotes: 4
Views: 6854
Reputation: 177860
You can try window.open which may be blocked. Also test you have a value.
onchange="var href = this.value; if (href) window.open(href,'_blank');"
Upvotes: 0
Reputation: 1237
Use the window.open function instead of the window.location variable. Window.location is meant to redirect the document itself. Window.open opens a new window.
Try window.open(this.options[this.selectedIndex].value)
instead.
<select id="select" onchange="window.open(this.options[this.selectedIndex].value);">
<option value="">Choose your state</option>
<option value="http://www.treasury.state.al.us/Content/index.aspx">Alabama</option>
<option value="http://treasury.dor.alaska.gov/">Alaska</option>
</select>
Upvotes: 9
Reputation: 4739
Try this:
<select id="select" onchange="var win = window.open(this.value, '_blank');win.focus();">
<option value="">Choose your state</option>
<option value="http://www.treasury.state.al.us/Content/index.aspx">Alabama</option>
<option value="http://treasury.dor.alaska.gov/">Alaska</option>
</select>
Check Fiddle
Upvotes: 0