简体   繁体   中英

Using innerHTML of <select> for windows.open()

On a page of my website I want user to select one choice of a and when they click on "connect" it open a new tab with the correct link.

code :

<select name="choice" id="choice">
    <option value="Server1.html">Server1</option>
    <option value="Server2.html">Server2</option>
    <option value="Server3.html">Server3</option>
</select>
    <input type="button" name="go_button" id= "go_button" value="go"   onclick="go_to_the_link()"/>
<script>
function go_to_this_link(){
    var element = document.getElementById("choice");
    var link = element.innerHTML;
    myWindow = window.open(link,"_blank");
}
</script>

According to the documentation this should works ... but since I'm new to JS and not expert in HTML I must have failed something. I want to use JS only and make something that also works with datalist.

Any help is welcome !

Solved: Ok I had 2 problem :

  1. In order to post this on stackoverflow I changed all my variable and function name, and I forgot to change one ...
  2. As said in the comment, I needed to use "value" and not innerHTML. I tried with value once but it also failed that's why I gave up this, I guess something else was wrong.

Thx for helping solving the problem !

(working) code :

<select name="choice" id="choice">
    <option value="Server1.html">Server1</option>
    <option value="Server2.html">Server2</option>
    <option value="Server3.html">Server3</option>
</select>
    <input type="button" name="go_button" id= "go_button" value="go"   onclick="go_to_the_link()"/>
<script>
function go_to_the_link(){
    var element = document.getElementById("choice");
    var link = element.value;
    myWindow = window.open(link,"_blank");
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM