Jeremy Dawson
Jeremy Dawson

Reputation: 43

Need help opening a link in a new tab from drop down menu

<select id="select" onchange="this.options[this.selectedIndex].value 
&amp;&amp; (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

Answers (3)

mplungjan
mplungjan

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

Gilles Lesire
Gilles Lesire

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

Sanjay Kumar N S
Sanjay Kumar N S

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

Related Questions