Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Links in <select> dropdown options

Is it possible for each dropdown options to link somewhere when selected without the need for an external button?

<select>   <option value="x">x</option>   <option value="y">y</option> </select> 
like image 588
Sebastian Avatar asked Sep 05 '12 18:09

Sebastian


People also ask

How do you hyperlink in a drop-down list?

Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.

Can we use HREF in select tag?

The <select> tag creates a dropdown list. You can't put html links inside a dropdown. Save this answer.

How do I redirect to another page from the option selected?

We can keep one dropdown listbox for the users to select one area ( option ) and then the script will redirect the page to that section. This is done by using onChange event of dropdown listbox. Once the user selection one option then by onChange event we run one function SelectRedirect().

How do you link options in HTML?

The <a> tag defines a hyperlink, which is used to link from one page to another. The most important attribute of the <a> element is the href attribute, which indicates the link's destination. By default, links will appear as follows in all browsers: An unvisited link is underlined and blue.


2 Answers

You can use the onChange property. Something like:

<select onChange="window.location.href=this.value">     <option value="www.google.com">A</option>     <option value="www.aol.com">B</option> </select> 
like image 70
Kevin Avatar answered Oct 20 '22 23:10

Kevin


Add an onchange event handler and set the pages location to the value

<select id="foo">     <option value="">Pick a site</option>     <option value="http://www.google.com">x</option>     <option value="http://www.yahoo.com">y</option> </select>  <script>     document.getElementById("foo").onchange = function() {         if (this.selectedIndex!==0) {             window.location.href = this.value;         }             }; </script> 
like image 32
epascarello Avatar answered Oct 20 '22 22:10

epascarello