Load page on selection from dropdown form

88,629

Solution 1

Try the following:

<select onchange="location = this.options[this.selectedIndex].value;">
    <option>Please select</option>
    <option value="http://www.apple.com/">Apple</option>
    <option value="http://www.bbc.com">BBC</option>
    <option value="http://www.facebook.com">Facebook</option>
</select>​

What you were looking for was 'onchange' instead of 'onsubmit'

Solution 2

Check out jQuery .change()

<script>
   $('select.menu').change(function(e){
      // this function runs when a user selects an option from a <select> element
      window.location.href = $("select.menu option:selected").attr('value');
   });
</script>

Solution 3

Something like this might help - basically you just tie an onChange event to the select so that when a new option is selected it'll forward the location to the page.

<p align="center">
<form name="jump" class="center">
<select name="menu" onchange="gotoPage(this)">
<option value="#">Select an option</option>
<option value="/link1.shtml">Link 1</option>
<option value="/link2.shtml">Link 2</option>
<option value="/link3.shtml">Link 3</option>
</select>
<input type="button" onClick="location=document.jump.menu.options[document.jump.menu.selectedIndex].value;" value="GO">
</form>
</p>

<script type="text/javascript">
function gotoPage(select){
    window.location = select.value;
}
</script>

Solution 4

I would recommend that you start using the javascript framework jQuery as it really will make your life much easier when it comes to javascript.

When you have jQuery installed and setup in you web page you should be able to do something like this:

<script type="text/javascript">
    $(document).ready(function() {
        $("#selection").change(function() {
            location = $("#selection option:selected").val();
        });
    });
</script>

<p align="center">
    <form name="jump" class="center">
        <select name="menu" id="selection>
            <option value="#">Select an option</option>
            <option value="/link1.shtml">Link 1</option>
            <option value="/link2.shtml">Link 2</option>
            <option value="/link3.shtml">Link 3</option>
        </select>
    </form>
</p>
Share:
88,629
JakeIC
Author by

JakeIC

Updated on October 11, 2020

Comments

  • JakeIC
    JakeIC over 3 years

    I am trying to make a drop down menu that takes me to various webpages. Right now it is set up so you make your selection and then you hit a "Go" button and then it takes you to the appropriate link. I am trying to figure out how to make it so when you make your selection it automatically goes and you don't need to push the "Go" button.

    Here is what I have:

    <p align="center">
    <form name="jump" class="center">
    <select name="menu">
    <option value="#">Select an option</option>
    <option value="/link1.shtml">Link 1</option>
    <option value="/link2.shtml">Link 2</option>
    <option value="/link3.shtml">Link 3</option>
    </select>
    <input type="button" onClick="location=document.jump.menu.options[document.jump.menu.selectedIndex].value;" value="GO">
    </form>
    </p>
    

    Any help or links to explanations would be great. Thank you!

  • racl101
    racl101 almost 6 years
    The best part about this approach is that it doesn't rely on JQuery and it's a nice simple, one-liner. Normally I don't love inlined JS, but here, it makes a lot of sense.
  • Jay Smoke
    Jay Smoke almost 3 years
    Brilliant. Works like magic and it's so simple