0
<html>
<head>
<script type="text/javascript">
window.addEventListener("load",go,false);
function go()
{
 var e = document.getElementById("dropdown");
 e.addEventLisnter("change",trigger,false);
}
function trigger(e)
{
 var url = e.option[e.selectedIndex].value;
 window.open(url);
 window.confirm("url successfull open");
}
</script>
</head>
<body>
<form action="#">
<select id="dropdown">
    <option value="">Choose a destination...</option>
    <option value="http://www.yahoo.com/">YAHOO</option>
    <option value="http://www.google.com/">GOOGLE</option>
    <option value="http://www.altavista.com/">ALTAVISTA</option>
    <option value="http://www.amazon.com/">AMAZON</option>
    <option value="http://artlung.com/">ARTLUNG</option>
</select>
</form>
</body>
</html>

i have visited many sites to find the solution for the code,my program is helping the user to open anew window once they selected the dropdown menu. i found out the syntax dealing with dropbox is selectedIndex and option. but when i apply it, it wouldn't function at all,can i know where is my mistake ? my reference link
Get selected value in dropdown list using JavaScript?
http://lab.artlung.com/dropdown/
jquery,onchange method are not allowed, it must use javascript and addEventListener

Community
  • 1
  • 1

1 Answers1

0

please update your js code as follows :

            <script type="text/javascript">
            window.addEventListener("load",go,false);


            function go()
            {
             var e = document.getElementById("dropdown");
             e.addEventListener("change",trigger,false);
            }
            function trigger()
            {
            var e = document.getElementById("dropdown");
             var url = e.options[e.selectedIndex].value;
             window.open(url);
             window.confirm("url successfull open");
            }
            </script>
Atul Nar
  • 695
  • 6
  • 10
  • I want to ask you something because I not sure with it why you use getElementById("dropdown") twice since it had been register in the first function call ? – Shinji Kagawa Nov 29 '14 at 13:00
  • i ran your code and in firebug is shows error that `ReferenceError: e is not defined` that means it cant access e in the trigger so thats why i called getElementByID second time in trigger function. However if you change line `e.option[e.selectedIndex].value` to this one `var url = this.options[this.selectedIndex].value;` your code will work without needing to reuse getElementByID – Atul Nar Nov 29 '14 at 14:39
  • can I know where you run your web programming code? I am using the browser to validate the coding so I don't know what kind of error in my code. To be more clear knowing where is my error, would you like to telling where you validate your code ? – Shinji Kagawa Nov 29 '14 at 16:19
  • Use firebug plugin for firefox in firebug inside console tab you will see your javascript errors.btw did new code works ?? – Atul Nar Nov 29 '14 at 16:43