0

I have a dropdown with links in them, when I select the option it will go to the page, but I want to use a button instead but not sure how to change the javascript to make this happen:

   <select name="form" onchange="location = this.options[this.selectedIndex].value;"class="form-control">
          <option>Choose a Service</option>
          <option value="http://www.google.com">Option 1</option>
          <option value="http://www.google.com">Option 2</option>
          <option value="http://www.google.com">Option 3</option>
          <option value="http://www.google.com">Option 4</option>
          <option value="http://www.google.com">Option 5</option>
        </select>
1
  • What you want is unclear. Could you please post an example of what you would want it to look like? Commented Nov 24, 2013 at 23:43

2 Answers 2

1

Instead of putting location = this.options[this.selectedIndex].value in unChange, move it to a button onClick event:

<select id="dropdown" name="form" class="form-control">
  <option>Choose a Service</option>
  <option value="http://www.google.com">Option-1</option>
  <option value="http://www.google.com">Option-2</option>
  <option value="http://www.google.com">Option-3</option>
  <option value="http://www.google.com">Option-4</option>
  <option value="http://www.google.com">Option-5</option>
</select>

<input type="button" value="Go!" onClick='location = document.getElementById("dropdown").options[document.getElementById("dropdown").selectedIndex].value'>
Sign up to request clarification or add additional context in comments.

Comments

0
   <select id="dd" name="form">
      <option>Choose a Service</option>
      <option value="http://www.google.com">Option 1</option>
      <option value="http://www.google.com">Option 2</option>
      <option value="http://www.google.com">Option 3</option>
      <option value="http://www.google.com">Option 4</option>
      <option value="http://www.google.com">Option 5</option>
    </select>

    <button onClick="launchPage" >

    <script>
      var launchPage = function() {
        var e = document.getElementById("dd");
        var url = e.options[e.selectedIndex].value;
        window.open(url);
      };
    </script>

2 Comments

This worked as well, I am not sure which one I should use though. Is it just a matter of preference or maybe convention?
Mostly preference. A lot of people would choose this one over the other because it is easier to maintain as your code expands. First one is the quick and dirty way, this one is the longer and cleaner maintenance way

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.