4

This works but I require it to open the link in a new window:

<select onChange="window.location=this.value">
<option value ="">select</option> 
<option value="http://www.google.com">google</option>
</select>

(I note this has been shown using a button and form but I require it to happen by selecting only)

2

2 Answers 2

8

Have you tried the window.open?

<select onChange="window.open(this.value)">
<option value ="">select</option> 
<option value="http://www.google.com">google</option>
</select>

https://jsfiddle.net/m9ts7tnj/

13
  • thanks but <select onChange="window.open=this.value" ... doesn't work
    – gungu
    Jan 19, 2016 at 3:34
  • you have to use parenthesis window.open(url)
    – hack3rfx
    Jan 19, 2016 at 3:37
  • please explain in the context of the select statement
    – gungu
    Jan 19, 2016 at 3:39
  • 1
    Every sample provided thus far works. Do you have pop-ups blocked? What browser are you using?
    – hack3rfx
    Jan 19, 2016 at 3:50
  • 1
    Use onclick. Read here.... stackoverflow.com/questions/12247368/…
    – hack3rfx
    Jan 19, 2016 at 3:57
2

You should pass the arguments through a function for extensibility and generally good coding practices.

JS Fiddle: https://jsfiddle.net/51hshn1h/

function openWindow(select) {
    var value = select.options[select.selectedIndex].value;
    window.open(value, 'newwindow')
}

<select onchange="openWindow(this)">
    <option value="">select an option</option>
    <option value="http://google.com">Google</option>
    <option value="http://yahoo.com">Yahoo!</option>
</select>
2
  • thank you, this works also. Ive used <select onClick="openWindow(this)"> rather than <select onchange="openWindow(this)"> as per Drew's suggestion to bypass the popup blocking issue I was having
    – gungu
    Jan 19, 2016 at 4:06
  • That's fine, but onClick on a select menu isn't the semantically correct way of doing it. To get around popup blockers, you should set the href of an a tag then trigger the click event on the a tag (and set the target to _blank). Jan 19, 2016 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.