Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Option value as url in select dropdown

Tags:

html

I am using the following as my nav menu in a 320px viewport (iPhone for example)

  <select>
     <option value="#">Home</option>
     <option value="#">About</option>
     <option value="#">Products and Services</option>
     <option value="#">Markets</option>
     <option value="#">Case Studies</option>
     <option value="#">Markets</option>
     <option value="#">Partners</option>
     <option value="#">Contact Us</option>
     <option value="#">Careers</option>
     <option value="#">News</option>
  </select>

I want to set the value as a link to the corresponding page. How do I go about setting this, I have put the URL in as the value but it does not work. Am I even approaching this correctly?

like image 538
Richlewis Avatar asked Feb 12 '26 23:02

Richlewis


1 Answers

If you use jQuery, you can do something like this:

$('select').change(function(){
    var url = $(this).val();
    window.location = url;
});

Add a change-event to (every) select element. When it it changed the function is run. The function takes the value from the newly selected option (in this case a url), and sets that url as the new location of the window.

like image 80
Krycke Avatar answered Feb 15 '26 13:02

Krycke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!