Dropdown list of each option to navigate to a different link

I have a dropdown and I want when I select an option and then click the Go button to take me to a different link (each option takes me to a different link)

This is my code, but it doesn't seem to play.

<html>
<head>
<title>
Database Project Winter 2012
</title>
<script>
function goToNewPage(dropdownlist)
 {
 var url = dropdownlist.options(dropdownlist.selectedIndex).value;
 if (url != "")
 {
 window.open(url);
 }
 }
</script>
</head>
<body>
<form name="dropdown">
 <select name="list" accesskey="E">
 <option selected>Select...</option>
 <option value="http://www.google.com/">Google</option>
 <option value="http://www.search.com/">Search.com</option>
 <option value="http://www.dogpile.com/">Dogpile</option>
 <select>
 <input type=button value="Go" onclick="goToNewPage(document.dropdown.list)">
</form>
</body>
</html>

      

What to change?

+3


source to share


2 answers


Have you considered using a simpler javascript function?

function goToNewPage() {
    if(document.getElementById('target').value){
        window.location.href = document.getElementById('target').value;
    }
}

      



And adding an id to the Select element?

<select name="list" id="target">

      

+2


source


You don't need to link inside the select tag. The value of the select tag itself contains the selected option



function goToNewPage(dropdownlist)
 {
 var url = dropdownlist.value;
 if (url != "")
 {
 window.open(url);
 }
 }

      

0


source







All Articles